【问题标题】:doctrine2: select error教义2:选择错误
【发布时间】:2013-02-18 11:41:02
【问题描述】:

在自定义 entityRepository 类中调用 entityRepository 的 find 方法时出现以下致命错误

致命错误:在 C:\Users\user\Desktop\projects\interview\application\libraries\Doctrine\ORM 中,未捕获的异常“Doctrine\ORM\OptimisticLockException”带有消息“无法在未版本化的实体 Entities\Comment 上获得乐观锁” \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 [内部函数]: 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\interview\索引.php( 203): 第 62 行 C:\Users\user\Desktop\projects\interview\application\libraries\Doctrine\ORM\OptimisticLockException.php 中的 require_once('C:\Users\user\D...')

这是我调用 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;
    }

我做错了什么??

【问题讨论】:

  • 我在教义和编程本身方面都是新手。无法找出实体未版本化的含义

标签: php codeigniter select doctrine-orm find


【解决方案1】:

从我读到的你有一个OptimisticLockException

this documentation中所说:

对对象进行版本检查时会引发 OptimisticLockException 通过版本字段使用乐观锁定失败。

您可以了解更多关于乐观锁here

我的猜测是它们与 $comment 变量冲突:

  1. 第一次初始化 $comment ($i=0) 时,comment#1 被加载
  2. 第二次(i=1,您找到comment#2,但comment 已经是一个实体并且被管理)$comment =... 尝试为comment#1 赋予comment#2 的值,甚至是uniq 的id ,因此您正在制造冲突。

试试这个:

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);
    }

这样您就可以确定您不会尝试重复使用以前的评论而不是新评论。

【讨论】:

  • 我试过你的建议,但无论如何我得到了这个例外。当我使用 parent::findby 代替 find 时,我没有例外。但是当你需要通过 id 查找时,这不是正确的编码方式,因为如果 ID 是主键,find 的执行时间比 findby for id 更短
  • 太棒了!这样可行!!谢谢你们,伙计们。但这种行为的原因是什么?
  • 当我更好地理解这一点时,也许我可以在 1 个月内回答,因为现在我对它的行为有一个大致的了解。你可以在学说的 Github 账号上提问。 @Ocramius 是 stackoverflow 的活跃成员,你可以问他我是 100% 他会给你答案
  • 非常感谢您的建议
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-10
  • 2013-05-30
  • 2012-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多