SELECT FOR UPDATE in Doctrine 2 DQL

doctrine-orm

Solution

Found the solution, using Query::setLockMode():

$query = $this->em->createQuery('SELECT ...');
$query->setLockMode(LockMode::PESSIMISTIC_WRITE);

Problem

I use pessimistic locking quite a lot in my application, and it works fine when using the Entity Manager: ``` $em->find($class, $id, LockMode::PESSIMISTIC_WRITE); ``` This results in a `SELECT FOR UPDATE` when using MySQL. Now I need to use the same locking but for entities retrieved with a DQL query. Is it possible to use pessimistic locking in DQL?

Original source