rkmax
rkmax

Reputation: 18133

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

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

Upvotes: 8

Views: 4420

Answers (2)

Reza S
Reza S

Reputation: 9748

You have to fully specify the entity name

$em->clear('Acme\MyBundle\Entity\EntityB');
$em->clear('Acme\MyBundle\Entity\EntityC');

Upvotes: 1

Jakub Zalas
Jakub Zalas

Reputation: 36201

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.

Upvotes: 1

Related Questions