I have entity Users, example
id, name, balance
1, Mike, 100
2, Alex, 20
I want to send 20 euro from Alex to Mike
$em = $this->getDoctrine()->getManager();
$mike = $em->getRepository('MyBundle:User')->find(1);
$alex = $em->getRepository('MyBundle:User')->find(2);
for($i=0;$i<2;$i++)
{
sleep(1);
try {
$em->getConnection()->beginTransaction();
$em->refresh($mike);
$em->refresh($alex);
$mike->setBalance($mike->getBalance() + 20);
$alex->setBalance($alex->getBalance() - 20);
$em->flush();
$em->getConnection()->commit();
} catch (\Exception $exception) {
$em->getConnection()->rollBack();
}
}
I need to be flush, only the first operation, but both work, because Alex is already with empty balance, but both operations work. Please help to understand how to solve this. Thanks!
Сборка персонального компьютера от Artline: умный выбор для современных пользователей