Let's say I want to do a bulk User update in my UsersController
.
In my UsersController
I do:
foreach ($users as $user) {
$userService = new UserService();
$user->updateUser($data);
}
If there are a lot of users, it can get slower because the UserService::updateUser method just do a persist()
/flush()
So I'm wondering if it could be a good idea to do something like that:
class UserService {
public function setUseTransaction($flag)
{
$this->useTransaction = $flag;
return $this;
}
public function updateUser($data)
{
// some data mapping
$entityManager->persist($user);
if ($this->useTransaction) {
$entityManager->flush();
}
}
public function commit()
{
$entityManager->flush();
}
}
Then in my UsersController
I can do:
$userService = new UserService();
$userService->setUseTransaction(true);
foreach ($users as $user) {
$userService = new UserService();
$user->updateUser($data);
}
$userService->commit();
What are your thought?