我已经读过我应该在持久化之前使用方法Commit,但这并没有解决我的问题。我也尝试在回滚之前使用取消EntityManager,但这也没有解决我的问题......
$this->em->getConnection()->beginTransaction(); // suspend auto-commit
if($out = $this->em->getConnection()->isTransactionActive()) {
var_dump($out);
}
try {
//... do some work
$history = new PromocodeHistory();
$history->setCode($code);
$history->setUser($this->getUser());
$this->em->persist($history);
$this->em->getConnection()->commit();
$this->em->flush();
$user = $this->getUser();
$updatedBalance = $user->getBalance() + $code->getSum() - $sum;
$user->setBalance($updatedBalance);
$this->em->persist($user);
$this->em->getConnection()->commit();
$this->em->flush();
$payment = new Payment();
$payment->setSum($sum);
$payment->setUser($this->getUser());
$payment->setPromocode($code);
....
} catch (\Exception $e) {
if(!$out = $this->em->getConnection()->isTransactionActive()) {
var_dump($out);
}
$this->em->getConnection()->rollBack();
}
在尝试阻止之前,isTransactionActive()返回true,但是在catch块中,这个方法会让我重新判断为false。 如何解决?
答案 0 :(得分:3)
我解决了我的问题:
$this->em->getConnection()->beginTransaction(); // suspend auto-commit
$this->em->getConnection()->setAutoCommit(false);