4
votes

I get the following fatal error while calling find method of entityRepository in a custom entityRepository class

Fatal error: Uncaught exception 'Doctrine\ORM\OptimisticLockException' with message 'Cannot obtain optimistic lock on unversioned entity Entities\Comment' in C:\Users\user\Desktop\projects\interview\application\libraries\Doctrine\ORM\OptimisticLockException.php:62 Stack trace: #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\interview\index.php(203): require_once('C:\Users\user\D...') in C:\Users\user\Desktop\projects\interview\application\libraries\Doctrine\ORM\OptimisticLockException.php on line 62

Here is method in which i call 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;
    }

What i'm doing wrong??

1
I'm quite new in doctrine and in programming itself. Can't find out what does mean that entity is unversionedFactory Girl

1 Answers

0
votes

From what i read you have an OptimisticLockException

As said in this documentation:

An OptimisticLockException is thrown when a version check on an object that uses optimistic locking through a version field fails.

You can find out more about Optimistic lock here

My guess is that their is a conflict with the $comment variable:

  1. the first time you initialize $comment ($i=0) comment#1 is loaded
  2. the second time (i=1, you find comment#2 but comment is already an entity and is manged) $comment =... tries to give comment#1 the values of comment#2 even the id that is uniq, so you are creating a conflict.

try this instead :

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

That way you are sure that you are not trying to re-use the previous comment instead of a new one.