I would like to use ON DUPLICATE KEY UPDATE
in Zend Framework 1.5, is this possible?
Example
INSERT INTO sometable (...)
VALUES (...)
ON DUPLICATE KEY UPDATE ...
I would like to use ON DUPLICATE KEY UPDATE
in Zend Framework 1.5, is this possible?
Example
INSERT INTO sometable (...)
VALUES (...)
ON DUPLICATE KEY UPDATE ...
As a sidebar, you can simplify the
ON DUPLICATE KEY UPDATE
clause and reduce the amount of processing your script needs to do by usingVALUES()
:See http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html for more information.
you can simply do something like this:
set unique index on your id
and then
@Bill Karwin: great solutions! But it would be greater if to use named placeholders (":id", ":col1", …) instead of questions signs. Than you wouldn’n need to duplicate values by array_marge. Also if to use "SET" syntax of "INSERT" instead of "VALUES", the code gets simplier to be generated automatically for any set of fields.
I worked for Zend and specifically worked on Zend_Db quite a bit.
No, there is no API support for the
ON DUPLICATE KEY UPDATE
syntax. For this case, you must simply usequery()
and form the complete SQL statement yourself.I do not recommend interpolating values into the SQL as harvejs shows. Use query parameters.
Edit: You can avoid repeating the parameters by using
VALUES()
expressions.USAGE:
eg. Model_Db_Contractors.php
IndexController.php
Use this instead:
This will update if exists or just insert if not. This is a part of the standard mysql api.