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

Edit
Report