How to get fresh document after persisting and flushing it in Doctrine ODM?

18 views Asked by At

I save few documents into database:

$this->documentManager->persist($item1);
$this->documentManager->persist($item2);
$this->documentManager->persist($item3);
$this->documentManager->flush();

Then I update some fields:

$queryBuilder = $this->documentManager->createQueryBuilder(Item::class);

$queryBuilder->updateMany()
  ->field('lockType')->set($lock)
  ->field('lockedAt')->set($now)
  ->field('id')->in($ids)
  ->getQuery()
  ->execute();

And then I experience some problem because when I get these fields from database I don't get documents with updated fields but old ones with values before update:

$results = $queryBuilder->field('lockType')->equals(Lock::PROCESSING)
  ->getQuery()
  ->toArray();

Array $results contains proper documents which have field lockType set to Lock::PROCESSING in database but when I look inside this array I see that documents have this field set to null and I don't understand why.

I tried to get these documents in another process and everything works properly. The problem occurs when I update and get documents in the same process. Does Doctrine ODM have some cache that I should clear before getting fresh documents from database? I also tried $this->documentManager->clear() after flushing documents but it results with exception: InvalidArgumentException: Document is not MANAGED.

0

There are 0 answers