Php Zend_Db_表-事务中删除后插入重复条目错误
我正在开发一个系统,其中有一个实体“程序”,通过链接表有一些“位置”。我正在使用Zend框架和Zend_Db_表 位置1------*程序位置---------1程序 在我的映射器中,我有一个函数save(),它应该保存程序:Php Zend_Db_表-事务中删除后插入重复条目错误,php,transactions,zend-db-table,duplicates,Php,Transactions,Zend Db Table,Duplicates,我正在开发一个系统,其中有一个实体“程序”,通过链接表有一些“位置”。我正在使用Zend框架和Zend_Db_表 位置1------*程序位置---------1程序 在我的映射器中,我有一个函数save(),它应该保存程序: public function save(Application_Model_Program $program) { try { //start transaction $this->_getDbTable()->
public function save(Application_Model_Program $program)
{
try
{
//start transaction
$this->_getDbTable()->getAdapter()->beginTransaction();
//make sure category is saved (has an id)
$categoryMapper = new Application_Model_Mapper_ProgramCategory();
$categoryMapper->save($program->getCategory());
$programData = array(
'title'=>$program->getTitle(),
'slug'=>$program->getSlug(),
'description'=>$program->getDescription(),
'dateFrom'=>$program->getDateFrom()->toString(self::DB_DATE_FORMAT),
'dateTo'=>$program->getDateTo()->toString(self::DB_DATE_FORMAT),
'category'=>$program->getCategory()->getId()
);
if($program->getId() === null)
{
$newId = $this->_getDbTable()->insert($programData);
$program->setId($newId);
}
else
{
$where = $this->_getDbTable()->getAdapter()->quoteInto("id = ?", $program->getId());
$this->_getDbTable()->update($programData, $where);
}
//save locations to program
$programProgramLocationMapper = new Application_Model_Mapper_Link_ProgramProgramLocation();
$programProgramLocationMapper->saveLocationsToProgram($program->getLocations(), $program);
//commit
$this->_getDbTable()->getAdapter()->commit();
}
catch(Exception $e)
{
//rollback transaction
$this->_getDbTable()->getAdapter()->rollBack();
throw $e;
}
}
应用程序\模型\映射程序\链接\程序ProgramLocation::saveLocationsToProgram方法如下所示:
public function saveLocationsToProgram(
array $locations,
Application_Model_Program $program)
{
$deleteWhere = $this->_getDbTable()->getAdapter()->quoteInto("program = ?", $program->getId());
$this->_getDbTable()->delete($deleteWhere);
foreach($locations as $location)
{
$data = array('program'=>$program->getId(), 'location'=>$location->getId());
$this->_getDbTable()->insert($data);
}
}
我的问题是,在最后显示的insert语句“Mysqli语句执行错误:键“PRIMARY”的重复条目“1-1”中出现异常。因此,插入之前的delete语句似乎没有正确执行。但是delete语句正确地返回已删除的条目数量,如果在delete之后执行select,则条目似乎已正确删除
同样的问题也发生在带有文件标记的类似结构中
我不知道是什么原因导致了这个问题,也不知道如何解决这个问题,如果你们中有人知道的话,那就太好了,因为它阻塞了系统的功能。我的应用程序中的某个错误导致程序
::$locations
数组包含重复项