Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 - doctrine2 batch processing

I have the following situation:

I need to create a large number of entities (Entity C) based on a pair of entities

  • Entity A (45)
  • Entity B (700000+)
  • Entity C (45 x 700000)
  • Entity D

So I decided to do the following:

$AEntities = $em->getRepository('MyBundle:EntityA')->findAll();
$DEntity = $em->getRepository('MyBundle:EntityD')->findOneBy($params);

$iterableResult = $em->getRepository('MyBundle:EntityB')
                ->createQueryBuilder('b')
                ->getQuery()->iterate();
$batchSize = 50

while (($row = $iterableResult->next()) !== false) {
  foreach($AEntities as $AEntity) {
    $entity = new Entity\EntityC();
    $entity->setEntityD($DEntity);
    $entity->setEntityB($row[0]);
    $entity->setEntityA($AEntity);
    $em->persist($entity);
  }

  if(($i % $batchSize) == 0){
    $em->flush();
    $em->clear();
  }
  $em->detach($row[0]);
  $i++;
}

$em->flush();

i follow instruction from doctrine2-batch-processing

but when i execute $em->detach($row[0]); and flush get an error A new entity was found through the relationship...

I have tried without $em->detach($row[0]); but this high memory consumption

I need: is to free the memory of each Entity B, after use, but at the same time each flush or by groups and not one by one, and clear all Entity C

like image 844
rkmax Avatar asked Dec 16 '11 22:12

rkmax


1 Answers

Calling clear() on entity manager detaches ALL objects (by default). Btw, you can pass entity name to detach entities of given type:

$em->clear('EntityB'); 
$em->clear('EntityC');

I think you're trying to detach already detached entity and therefore it's treated as new.

Try removing clear() call. You might also try removing detach() call and call clear() on selected entities.

like image 65
Jakub Zalas Avatar answered Nov 27 '22 04:11

Jakub Zalas