Is there a way to do "INSERT ... ON DUPLICATE KEY UDPATE" 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 ... 
+44
php mysql zend-framework insert-update
Nov 19 '08 at 16:44
source share
6 answers

I worked at 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 should simply use query() and compose the complete SQL statement yourself.

I do not recommend interpolating values ​​in SQL, as harvays shows. Use query parameters.

Edit: you can avoid repeating parameters with 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"); 
+49
Nov 19 '08 at 17:46
source share

As a sidebar, you can simplify the ON DUPLICATE KEY UPDATE and reduce the processing volume of your script with 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/insert-on-duplicate.html for more details.

+6
Jul 30 '09 at 14:44
source share

@Bill Karwin: great solutions! But it would be better if instead of the names of characters to use the named placeholders (": id", ": col1", ...). Than you will not need to duplicate the values ​​of array_marge. In addition, if you use the "SET" syntax for "INSERT" instead of "VALUES", it becomes easier to code automatically for any set of fields.

 $sql = 'INSERT INTO sometable SET id = :id, col2 = :col2, col3 = :col3 ON DUPLICATE KEY UPDATE id = :id, col2 = :col2, col3 = :col3'; 
+5
Jul 28 '09 at 7:10
source share
 $arrayData = array('column1' => value1, 'column2' => value2, ...) class Model_Db_Abstract extends Zend_Db_Table_Abstract { protected $_name; protected $_primaryKey; public function insertOrUpdate($arrayData) { $query = 'INSERT INTO `'. $this->_name.'` ('.implode(',',array_keys($arrayData)).') VALUES ('.implode(',',array_fill(1, count($arrayData), '?')).') ON DUPLICATE KEY UPDATE '.implode(' = ?,',array_keys($arrayData)).' = ?'; return $this->getAdapter()->query($query,array_merge(array_values($arrayData),array_values($arrayData))); } } 

APPLICATION:

eg. Model_Db_Contractors.php

 class Model_Db_Contractors extends Model_Db_Abstract { protected $_name = 'contractors'; protected $_primaryKey = 'contractor_id'; ... } 

IndexController.php

 class IndexController extends Zend_Controller_Action { public function saveAction() { $contractorModel = new Model_Db_Contractors(); $aPost = $this->getRequest()->getPost(); /* some filtering, checking, etc */ $contractorModel->insertOrUpdate($aPost); } } 
+3
Apr 09 '13 at 11:49 on
source share

Use this instead:

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

It will be updated if exists or just paste if not. This is part of the standard mysql api.

+1
Sep 07 '11 at 19:27
source share

you can just do something like this:

set a unique identifier for your id

and then

 try { do insert here } catch (Exception $e) { do update here } 
-6
Mar 12 '10 at 19:14
source share



All Articles