更新实体对象时,无法在foreach循环外调用entityManager-> flush()

I have a bunch of user-objects need to add a role to admin but after a foreach loop, I call entitymanager->flush() but it threw an error out.

I'm using symfony 2.7. Here is my code:

try {
    foreach ($users as $user) {
        $output->writeln(sprintf('<comment>processing user <fg=green>%s</fg=green> </comment>', $user->getEmail()));
        $user->addRole(User::ROLE_ADMIN);
        // $em->persist($user);
    }
    $em->flush();
} catch (Symfony\Component\Debug\Exception\ContextErrorException $e) {
    dump($e->getMessage()) ;
}

I got this below exception:

[Symfony\Component\Debug\Exception\ContextErrorException]
  Catchable Fatal Error: Argument 3 passed to Doctrine\ORM\Event\PreUpdateEventArgs::__construct() must be of the
  type array, null given, called in .../vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.ph
  p on line 1009 and defined

When I put $em->flush() inside the foreach, it works though. I tried to persist the object inside the loop but still got the mentioned exception. Does anyone have any idea? Thanks!