Description
Feature Request
Q | A |
---|---|
New Feature | yes |
RFC | yes |
BC Break | no |
Summary
I have some routines where I have to iterate over 3 millions objects.
Because of this use a cursor, where I iterate through mongo collection using doctrine with hydration true and partial objects. And for every X elements retrieved we clean the document manager in order to avoid memory going up and up.
One day we found a bug in our code, where objects were being updated in the background, and when calling flush in another collection, the collection iterated was being changed, due to a mistake made with default value in the class definition.
Therefore we decided to add readOnly
in our queries for security (we fixed the original bug). But, by doing this, the clear document manager doesn't work anymore, since the documents are not registered in the UnitOfWork. So our memory grow and grow until it throws an OutOfMemoryException.
We've searched everywhere, but haven't find a way to clear the doctrine cache.
$query = $this->repository->createQueryBuilder()
->readOnly()
->getQuery();
/** @var Cursor $cursor */
$cursor = $query->execute();
$cursor->batchSize($batchSize);
$iterator = 0;
foreach($cursor as $product){
$i++;
// ... operations within the product ...
if($i % $batchSize === 0){
$this->repository->clear(); // DOES NOT WORK WHEN USING READ ONLY
}
}
I'd like to know if it's possible to somehow clear this cache made by doctrine as it works for notReadOnly objects.