I am working with Zend Framework 1.12.1
I am trying to use a "save" method on a row from Zend_Db_Table_Abstract. As I understand this method should automatically decide whether to save or update a row in a table. I created two forms, one is for creating a record in database, another is for updating. It works perfect with create a new record. Method uses INSERT MySQL command. When I am trying to update the row, instead of choosing MySQL command UPDATE, save() method still chooses INSERT and duplicates a record in the table.
Here's a full error log:
Message: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'France' for key 'country'
Stack trace:
#0 C:\wamp\zf\library\Zend\Db\Statement.php(303): Zend_Db_Statement_Pdo->_execute(Array)
#1 C:\wamp\zf\library\Zend\Db\Adapter\Abstract.php(480): Zend_Db_Statement->execute(Array)
#2 C:\wamp\zf\library\Zend\Db\Adapter\Pdo\Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO `co...', Array)
#3 C:\wamp\zf\library\Zend\Db\Adapter\Abstract.php(576): Zend_Db_Adapter_Pdo_Abstract->query('INSERT INTO `co...', Array)
#4 C:\wamp\zf\library\Zend\Db\Table\Abstract.php(1076): Zend_Db_Adapter_Abstract->insert('countries', Array)
#5 C:\wamp\zf\library\Zend\Db\Table\Row\Abstract.php(467): Zend_Db_Table_Abstract->insert(Array)
#6 C:\wamp\zf\library\Zend\Db\Table\Row\Abstract.php(438): Zend_Db_Table_Row_Abstract->_doInsert()
#7 C:\wamp\www\website\library\App\Data.php(34): Zend_Db_Table_Row_Abstract->save()
#8 C:\wamp\www\website\application\controllers\CountryController.php(82): App_Data->save()
#9 C:\wamp\zf\library\Zend\Controller\Action.php(516): CountryController->editAction()
#10 C:\wamp\zf\library\Zend\Controller\Dispatcher\Standard.php(308): Zend_Controller_Action->dispatch('editAction')
#11 C:\wamp\zf\library\Zend\Controller\Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#12 C:\wamp\zf\library\Zend\Application\Bootstrap\Bootstrap.php(97): Zend_Controller_Front->dispatch()
#13 C:\wamp\zf\library\Zend\Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#14 C:\wamp\www\website\public\index.php(26): Zend_Application->run()
#15 {main}
I am trying to update a record in the table {'id'=>'1', 'country'=>'France', 'continent'=>'Europe', 'created'=>'2013-03-07 10:10', 'modified'=>''}
Is there way to force method save() to choose Zend_Db_Table_Row_Abstract->_doUpdate() method instead of _doInsert()?
Thak you very much.
idfield it will update the record rather try to insert the new one. - Rikesh([id] => 1, [country] => France, [continent] => Europe, [created] => 2013-03-06 19:27:09, [modified] => ). After changes are made, Zend_Db_Table_Row_Abstract looks like this:( 'controller' => 'country', 'action' => 'edit', 'id' => '1', 'module' => 'default', 'country' => 'France', 'continent' => 'Europe', 'created' => '2013-03-06 19:27:09', 'modified' => '2013-03-07 00:00:00', 'submit' => 'Add Country', )- Midnight CoderZend_Db_Table_Rowwill update only if yourZend_Db_Table_Rowwas previously fetched from the database, if you create a newZend_Db_Table_Row, even if it matches an existing PK or UNIQUE key, it will save it through anINSERTand not anUPDATE- Matteo Tassinari$this->_row = $this->_dbTable->find($id)->current()where id is the country id. _row is protected variable inside Country model. - Midnight Coder