I must use "ON DUPLICATE KEY UPDATE" in Zend Framework, is possible?


Place INTO sometable (...) VALUES (...) ON DUPLICATE KEY UPDATE ...

I labored for Zend and particularly done Zend_Db a great deal.

No, there's no API support for that ON DUPLICATE KEY UPDATE syntax. With this situation, you have to simply employ query() and make up the complete SQL statement yourself.

I don't recommend interpolating values in to the SQL as harvejs shows. Use query parameters. Note you need to repeat their email list of values:

$sql = "Place INTO sometable (id, col2, col3) VALUES (?, ?, ?)

  ON DUPLICATE KEY UPDATE id = ?, col2 = ?, col3 = ?"

$values = array("id"=>1, "col2"=>327, "col3"=>"active")

$db->query($sql, array_merge(array_values($values), array_values($values)))

I incorporated id = ? despite the fact that it's clearly a no-op when the bottom line is a replica, but it is handy to incorporate it since it is simpler to clone the variety of values after i do not have to take away the first entry.

Like a sidebar towards the recognized solutions about this thread, you are able to simplify the ON DUPLICATE KEY UPDATE clause and lower the quantity of processing your script must do by utilizing VALUES():

$sql = 'INSERT INTO ... ON DUPLICATE KEY UPDATE id = VALUES(id), col2 = VALUES(col2), col3 = VALUES(col3)'

See http://dev.mysql.com/doc/refman/5.1/en/place-on-duplicate.html to learn more.

@Bill Karwin: great solutions! But it might be greater if to make use of named placeholders (":id", ":col1", …) rather than questions signs. Than you wouldn’n have to duplicate values by array_marge. And if to make use of "SET" syntax of "Place" rather than "VALUES", the code will get simplier to become produced instantly for just about any group of fields.

$sql = 'INSERT INTO sometable SET id = :id, col2 = :col2, col3 = :col3

    ON DUPLICATE KEY UPDATE id = :id, col2 = :col2, col3 = :col3'

Make use of this rather:

REPLACE INTO sometable SET area ='value'.....

This can update if is available or simply place otherwise. This really is an element of the standard mysql api.

I had been trying to find this for any very long time, but finally I discovered an response to create the SQL query. Something similar to:

$db->query('INSERT INTO table (











            column = "'.$value.'"


I've not found another solution.

you can just make a move such as this:

set unique index in your id

after which

try place here

 catch (Exception $e) 

In case your doing something similar to this, then probably your design is problematic.