Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php doctrine2:选择错误_Php_Codeigniter_Select_Doctrine Orm_Find - Fatal编程技术网

Php doctrine2:选择错误

Php doctrine2:选择错误,php,codeigniter,select,doctrine-orm,find,Php,Codeigniter,Select,Doctrine Orm,Find,在自定义entityRepository类中调用entityRepository的find方法时出现以下致命错误 致命错误:未捕获异常“条令\ORM\OptimisticLockException”,并在C:\Users\user\Desktop\projects\Session\application\libraries\条令\ORM\OptimisticLockException.php:62堆栈跟踪:0中显示消息“无法获取未版本化实体\Comment上的乐观锁定”C:\Users\user

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

致命错误:未捕获异常“条令\ORM\OptimisticLockException”,并在C:\Users\user\Desktop\projects\Session\application\libraries\条令\ORM\OptimisticLockException.php:62堆栈跟踪:0中显示消息“无法获取未版本化实体\Comment上的乐观锁定”C:\Users\user\Desktop\projects\interview\application\libraries\Doctrine\ORM\EntityRepository.php140:Doctrine\ORM\OptimisticLockException::notVersioned'Entities\commo…'1C:\Users\user\Desktop\projects\interview\application\models\Repositories\CommentRepository.php24:Doctrine\ORM\EntityRepository->find'Entities\commo,1 2 C:\Users\user\Desktop\projects\interview\application\controllers\CommentController.php65:Repositories\comments->activateByIdsArray 3[内部函数]:comments 4 C:\Users\user\Desktop\projects\interview\system\core\CodeIgniter.php359:call\u user\u func\u arrayArray,数组5 C:\Users\user\Desktop\projects\interview\index.php203:在第62行的C:\Users\user\Desktop\projects\interview\application\libraries\doctor\ORM\OptimisticLockException.php中需要一次'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;
    }

我做错了什么???

从我读到的来看,你有一个乐观的例外

如中所述:

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

您可以了解有关乐观锁的更多信息

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

第一次初始化$comment$i=0时,将加载comment1 第二次i=1时,您会发现comment2,但comment已经是一个实体,并且管理$comment=。。。尝试为comment1提供comment2的值,甚至是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要短!真管用!!谢谢各位。但这种行为的原因是什么?也许我能在一个月内回答,当我更好地理解这一点,因为现在我对它的行为有了一个大致的了解。您可以在doctrine的Github帐户上提问@Ocramius是stackoverflow的活跃成员,你可以问他我是100%,他会给你答案的。非常感谢你的建议