2013-03-07 44 views
1

我与Zend框架1.12.1一个Zend_Db_Table_Row ::保存()不更新表

我试图用一个“拯救”的方法从Zend_Db_Table_Abstract一排工作。据我所知,这种方法应该自动决定是否保存或更新表中的一行。我创建了两种形式,一种是在数据库中创建记录,另一种是为了更新。 它创建一个新的记录完美。方法使用INSERT MySQL命令。当我尝试更新行时,不是选择MySQL命令UPDATE,save()方法仍然选择INSERT并在表中复制记录。

这里是一个完整的错误日志:

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} 

我想在表中更新记录{ '身份证'=> '1', '国家'=> '法国', '大陆'= > '欧洲', '创造'=> '2013年3月7日10:10', '修改'=> ''}

有没有办法强制方法Save()选择Zend_Db_Table_Row_Abstract - > _ doUpdate()方法而不是_doInsert()?

非常感谢。

+0

显示您正在保存的数据数组?如果您设置了'id'字段,它会更新记录,而不是尝试插入新记录。 – Rikesh 2013-03-07 09:48:00

+0

调用窗体的数组是Array([id] => 1,[country] => France,[continent] => Europe,[created] => 2013-03-06 19:27:09,[modified ] =>)'。更改完成后,Zend_Db_Table_Row_Abstract看起来像这样:'( 'controller'=>'country', 'action'=>'edit', 'id'=>'1', 'module'=>'default ', 'country'=>'法国', 'continent'=>'欧洲', 'created'=>'2013-03-06 19:27:09', 'modified'=>'2013- '03-07 00:00:00', 'submit'=>'添加国家', )' – 2013-03-07 10:05:57

+0

只有当您的'Zend_Db_Table_Row'先前从数据库中获取时,Zend_Db_Table_Row'才会更新,如果您创建了新的'Zend_Db_Table_Row ',即使它匹配现有的PK或UNIQUE键,它将通过“INSERT”而不是“UPDATE”来保存它。 – 2013-03-07 10:08:46

回答

0

您可能需要更新您的问题以添加一些代码,因为目前我们都在猜测。

下面是save()如何在映射器中工作的简单示例,使用实体模型代替数组。

public function saveArtist(Music_Model_Artist $artist) 
    { 
     //if id is set fetch the row 
     if (!is_null($artist->id)) { 
      $select = $this->getGateway()->select(); 
      $select->where('id = ?', $artist->id); 
      $row = $this->getGateway()->fetchRow($select); 
     } else { 
      //if id is not set create a new row 
      $row = $this->getGateway()->createRow(); 
     } 
     // this data is added to the record always 
     $row->name = $artist->name; 
     //save/update the row 
     $row->save(); 
     //return the whole row, my preference as save() just returns primary key of row. 
     return $row; 
    } 

真正发生的,如果你,如果你创建一个新的行save()INSERT提交的现有行save()UPDATE是。这种方法没有什么魔法可以根据传递给它的数据来控制它的功能。具体而言,它的效果很好。

您的应用程序出现,当你打算为它做一个UPDATE在做一个INSERT