51

I'm using doctrine/mongodb 1.0.0-BETA1 in a symfony2.1 install.

So i'm trying to force my repository to call data from my database instead of using the object it has cached.

$audit = $dm->getRepository("WGenSimschoolsBundle:Audit")->findOneById("xxxx");

.... do something somewhere to change the object ....

At this point if I call

$audit = $dm->getRepository("WGenSimschoolsBundle:Audit")->findOneById("xxxx");

The audit data hasn't changed. It still has the object it originally fetched. If I try to

$dm->refresh($audit) 

I get the same thing. Is there anyway for me to go back to the database for the value?

daSn0wie
  • 879
  • 1
  • 8
  • 15
  • I think doctrine doesn't use caching by default.. Have you overriden the findOneById method? – fkoessler Feb 10 '13 at 07:40
  • i'm wondering if the php-mongo driver caches the results... i couldn't find anything in the doctrine documentation that talked about caching either – daSn0wie Feb 11 '13 at 19:51

7 Answers7

63

Have you flushed your changes to the $audit object?

$audit = $dm->getRepository("WGenSimschoolsBundle:Audit")->findOneById("xxxx");
//do something somewhere to change the object
$dm->flush();

Every time you do a findBy(...) or findOneBy(...) it actually fetches a new document from the DB. (you should see the query in the Symfony profiler)

With a find() instead, it will fetch the document from his internal proxy cache. The Documents stay in the proxy cache until you call the $dm->clear() method.

stanhope
  • 136
  • 1
  • 8
Madarco
  • 2,084
  • 18
  • 26
  • 26
    In my case (see my answer), the `findOneBy` did use cache too ! I had to use `$em->clear();` – achedeuzot Apr 17 '14 at 10:05
  • Doctrine ORM has a "Query cache", that ODM doesn't (actually) have, are you sure your result didn't came from that cache? – Madarco Apr 18 '14 at 08:56
  • 4
    Clear remove all entities saved in the cache, so if a the user is persisted (for exemple), doctrine will throw an error because it does not know this existing user and doctrine propose to apply a "Cascade persist" in order to add it. But the user already exist. So this solution is not effective in that case. According to me, the best solution is to use `$entityManager->refresh($entity);` in order to force reload the entity directly from the database. – Nicolas Facciolo Jan 04 '21 at 15:27
46

This worked for me:

$doc = $this->documentManager->getRepository('MyBundle:MyDoc')->find($id);

/* ... in the meanwhile another external process is doing some changes to the object ...*/
$doc = $this->documentManager->getRepository('MyBundle:MyDoc')->find($id); // Perhaps this is not useful
$this->documentManager->refresh($doc);
fdellutri
  • 963
  • 1
  • 7
  • 16
29

Adding to the previous answers, I was searching how to refresh the database of an Entity, not of a Document but the solution was close. I'm posting it here for others stumbling on this page with the same problem.

In one of my functional tests, I was using two times the same query:

$em = $kernel->getContainer()->get('doctrine.orm.entity_manager');
$user = $em->getRepository('AcmeUserBundle:User')->findOneBy(array('email' => 'james.bond@secure.gov.co.uk'));
echo "Old hash: ".$user->getPassword() . "\n";
// result: 8bb6118f8fd6935ad0876a3be34a717d32708ffd

Then the tests goes through a process of changing the password. I then re-queried the user to compare if the password hash had changed with the same query:

$user = $em->getRepository('AcmeUserBundle:User')->findOneBy(array('email' => 'james.bond@secure.gov.co.uk'));
echo "New hash: ".$user->getPassword() . "\n";
// result: 8bb6118f8fd6935ad0876a3be34a717d32708ffd # Same !

The problem was that even though the tested controller updated the hash, the entity manager had the entity in cache.

So, the solution was to add the following between the two queries:

$em->clear();

And now, the password hash changed between queries ! Yay !

achedeuzot
  • 4,164
  • 4
  • 41
  • 56
  • Why to clear the whole cache instead of just reloading/refresh the entity object from data source? – MAZux Mar 03 '20 at 12:33
  • Because at the time of writing this, I didn't know there was that option. Maybe there's a more granular way the flush/refresh. – achedeuzot Mar 03 '20 at 14:31
9

You can use method refresh :

$post; # modified
$entityManager->refresh();
$post; # reset from db
حمید
  • 143
  • 1
  • 6
  • 1
    This works by accident only. `refresh` reloads a single entity from the data source, you're omitting a required parameter in your example. You should call `$em->clear()` to detach all entities instead. – Niels Keurentjes Mar 17 '18 at 00:33
3

When you deal with related entities, if you modify the related entities and save them through the parent object, you will have to add an option cascade={"detach"} for the detach to be effective.

For example, let's say you want to update a list of Friends on a Person object, by adding new objects to the list, removing some of them and updating some existing ones. You're going to have

$em->flush();
$em->detach($entity);

And in your Person entity make sure to update your friends relation :

@ORM\OneToMany(targetEntity="Somewhere\PeopleBundle\Entity\Person", mappedBy="person", cascade={"detach"})
private $friends;
vbourdeix
  • 71
  • 6
1

Try something like

$dm->getUnitOfWork()->clear('WGenSimschoolsBundle:Audit');
ryabenko-pro
  • 728
  • 1
  • 7
  • 18
0

You need to use EntityManager#clear(). See docs.

Roman Zagday
  • 111
  • 1
  • 10