7

I am trying out the batch processing method described here: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/batch-processing.html

my code looks like this

    $limit = 10000;
    $batchSize = 20;
    $role = $this->em->getRepository('userRole')->find(1);
    for($i = 0; $i <= $limit; $i++)
    {
        $user = new \Entity\User;
        $user->setName('name'.$i);
        $user->setEmail('email'.$i.'@email.blah');
        $user->setPassword('pwd'.$i);
        $user->setRole($role);
        $this->em->persist($user);
         if (($i % $batchSize) == 0) {
             $this->em->flush();
             $this->em->clear();
        }
    }

the problem is, that after the first call to em->flush() also the $role gets detached and for every 20 users a new role with a new id is created, which is not what i want

is there any workaround available for this situation? only one i could make work is to fetch the user role entity every time in the loop

thanks

luiges90
  • 4,493
  • 2
  • 28
  • 43
bazo
  • 745
  • 9
  • 26
  • I know it's late, but - the clear() also accepts a parameter, so clear only the User entity: `$this->em->clear(\Entity\User::class);` – userfuser Jul 15 '20 at 18:07

2 Answers2

15

clear() detaches all entities managed by the entity manager, so $role is detached too, and trying to persist a detached entity creates a new entity.

You should fetch the role again after clear:

$this->em->clear();
$role = $this->em->getRepository('userRole')->find(1);

Or just create a reference instead:

$this->em->clear();
$role = $this->em->getReference('userRole', 1);
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
0

As an alternative to arnaud576875's answer you could detach the $user from the entity manager so that it can be GC'd immediately. Like so:

$this->em->flush(); 
$this->em->detach($user); 

Edit:
As pointed out by Geoff this will only detach the latest created user-object. So this method is not recommended.

Benjamin Cremer
  • 4,842
  • 1
  • 24
  • 30