Do DQL queries update objects already on the identity map?

Let's say I load a user by id:

$user = $em->find('Domain\Model\User', 123);

      

And now run a DQL query to select multiple users, including this already known user:

$users = $em->createQuery('SELECT u FROM Domain\Model\User u')->getResult();

      

If user 123 has changed in the database between these two requests (assuming I am not included in the transaction REPEATABLE READ

), will this request update user 123 with the fresh data returned by the query, or will it just return an object from the identity card while ignoring the new data?

+3


source to share


1 answer


After testing this exact use case, it turns out that Doctrine 2 does not update the existing object with the data returned by the DQL query, and only returns it as is from the id map .

Finally, I found relevant documentation to support this:

In normal operation, a result set that loads the data of an existing object is discarded in favor of the already existing object.

It also provides the ability to force the DQL query to update the object using Query::HINT_REFRESH

:



If you provide this hint and the query returns data for an object that is already managed by UnitOfWork, the fields of the existing object will be updated.

It's pretty handy and easy to use:

use Doctrine\ORM\Query;

$users = $em->createQuery('SELECT u FROM Domain\Model\User u')
    ->setHint(Query::HINT_REFRESH, true)
    ->getResult();

      

+10


source







All Articles