How to stop Doctrine 2 from caching a result in Symfony 2? How to stop Doctrine 2 from caching a result in Symfony 2? symfony symfony

How to stop Doctrine 2 from caching a result in Symfony 2?


It's a normal behavior.

Doctrine stores a reference of the retrieved entities in the EntityManager so it can return an entity by it's id without performing another query.

You can do something like :

$entityManager = $this->get('doctrine')->getEntityManager();$repository = $entityManager->getRepository('KnowledgeShareBundle:Post');$post = $repository->find(1);$entityManager->detach($post);// as the previously loaded post was detached, it loads a new one$existingPost = $repository->find(1);

But be aware of that as the $post entity was detached, you must use the ->merge() method if you want to persist it again.


You can also use the refresh method, which refreshes the persistent state of an entity from the database, overriding any local changes that have not yet been persisted.Something like:

$entityManager = $this->get('doctrine')->getEntityManager();$repository = $entityManager->getRepository('KnowledgeShareBundle:Post');$post = $repository->find(1);$entityManager->refresh($post);

now $post contains the last version from database.