1

我正在使用 Zend Framework 1.12.1

我正在尝试对 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}  

我正在尝试更新表 {'id'=>'1', 'country'=>'France', 'continent'=>'Europe', 'created'=>'2013-03-07 10 中的记录:10', '修改'=>''}

有没有办法强制方法 save() 选择 Zend_Db_Table_Row_Abstract->_doUpdate() 方法而不是 _doInsert()?

非常感谢。

4

1 回答 1

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()UPDATE如果您创建新save()行将INSERT。这种方法没有任何魔力,您可以根据传递给它的数据来控制它的作用。具体一点,效果很好。

当您打算让它执行UPDATE时,您的应用似乎正在执行INSERT

于 2013-03-08T11:05:51.003 回答