Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to do an "INSERT...ON DUPLICATE KEY UPDATE" in Zend Framework 1.5?

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 ... 
like image 799
danielrsmith Avatar asked Nov 19 '08 16:11

danielrsmith


People also ask

Is insert on duplicate key update Atomic?

So yes it is atomic in the sense that if the data that you are trying to insert will cause a duplicate in the primary key or in the unique index, the statement will instead perform an update and not error out.

How does on duplicate key update work?

ON DUPLICATE KEY UPDATE is a MariaDB/MySQL extension to the INSERT statement that, if it finds a duplicate unique or primary key, will instead perform an UPDATE. The row/s affected value is reported as 1 if a row is inserted, and 2 if a row is updated, unless the API's CLIENT_FOUND_ROWS flag is set.


1 Answers

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 use query() 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.

$sql = "INSERT INTO sometable (id, col2, col3) VALUES (:id, :col2, :col3)   ON DUPLICATE KEY UPDATE col2 = VALUES(col2), col3 = VALUES(col3)";  $values = array("id"=>1, "col2"=>327, "col3"=>"active"); 
like image 71
Bill Karwin Avatar answered Sep 24 '22 11:09

Bill Karwin