3

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??

Factory Girl
  • 910
  • 4
  • 18
  • 29

1 Answers1

0

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.

Jeffrey Nicholson Carré
  • 2,950
  • 1
  • 26
  • 44
  • I tried your suggestion, but anyways i get this exception. I don't have exception when i use parent::findby instead find. But it is not the right way to code when you need to find by id, because execution time of find is shorter then findby for id if ID is primary key – Factory Girl Mar 05 '13 at 05:24
  • Great! That works!! Thank you, men. But what is the reason of such behavior? – Factory Girl Mar 05 '13 at 06:05
  • Maybe i will be able to answer in 1 month when i understand this better for now i have a general idea of how it behaves. You can ask the question on doctrine's Github account. @Ocramius is an active member of stackoverflow you can ask him i am 100% he will give you the answer – Jeffrey Nicholson Carré Mar 05 '13 at 19:06