在自定义entityRepository类
中调用entityRepository的find方法时出现以下致命错误致命错误:未捕获的异常'Doctrine \ ORM \ OptimisticLockException',消息'无法在C:\ Users \ user \ Desktop \ projects \ interview \ application \ libraries \ Doctrine \ ORM中获取无版本实体实体\注释的乐观锁定\ OptimisticLockException.php:62堆栈跟踪:#0 C:\ Users \ user \ Desktop \ projects \ interview \ application \ libraries \ Doctrine \ ORM \ EntityRepository.php(140):Doctrine \ ORM \ OptimisticLockException :: notVersioned('Entities \ Commen ...')#1 C:\ Users \ user \ Desktop \ projects \ interview \ application \ models \ Repositories \ CommentRepository.php(24):Doctrine \ ORM \ EntityRepository-> find('Entities \ Commen。 ..',1)#2 C:\ Users \ user \ Desktop \ projects \ interview \ application \ controllers \ CommentController.php(65):Repositories \ CommentRepository-> activateByIds(Array)#3 [internal function]:CommentController - > approveComments()#4 C:\ Users \ user \ Desktop \ projects \ interview \ system \ core \ CodeIgniter.php(359):call_user_func_array(Array,Array)#5 C:\ Users \ user \ Desktop \ projects \访谈\我ndex.php(203):require_once('C:\ Users \ user \ D ...')在C:\ Users \ user \ Desktop \ projects \ interview \ application \ libraries \ Doctrine \ ORM \ OptimisticLockException.php上行62
这是我调用find
的方法public function activateByIds($arrayOfIds){
if (count($arrayOfIds)>=1) {
for ($i=0; $i<count($arrayOfIds); $i++){
$comment = parent::find('Entities\Comment', $arrayOfIds[$i]);
$comment->setIsactive(1);
$this->_em->merge($comment);
$this->_em->flush();
}
return true;
}
else return false;
}
我做错了什么?
答案 0 :(得分:0)
根据我的阅读,你有一个OptimisticLockException
正如this documentation所说:
对对象进行版本检查时抛出OptimisticLockException 通过版本字段使用乐观锁定失败。
您可以找到有关乐观锁here
的更多信息我的猜测是他们与$ comment变量冲突:
试试这个:
public function activateByIds($arrayOfIds){
$comments =array();
if (count($arrayOfIds)>=1) {
foreach($arrayOfIds as $i=>$id){
$comments [$i] = $this->getEntityManager()->find('Entities\Comment', $id); //new comment, not the old one!
$comments [$i]->setIsactive(1);
$this->_em->merge($comments[$i]);
$this->_em->flush();
}
return true;
}
else return false;
unset ($comments);
}
这样您确定不会尝试重复使用之前的评论而不是新评论。