为了确保我的操作能够正确处理事务,我发现自己在我的 Controller 中一遍又一遍地重复这段代码:
/**
* @Route("/complete", name = "authentication_complete")
*/
public function completeAction(Request $request)
{
$result = null;
try {
$this->getManager()->beginTransaction();
$result = $this->doCompleteAction($request);
$this->getManager()->flush();
$this->getManager()->commit();
} catch (\Exception $e) {
// @codeCoverageIgnoreStart
$this->getManager()->rollback();
throw $e;
// @codeCoverageIgnoreEnd
}
return $result;
}
public function doCompleteAction(Request $request)
{
// do whatever you action is suposed to do
return $response;
}
我想要类似@ManageTransaction
的东西。这将进入 Action 的评论并为我节省了很多重复代码。在一个完美的世界中,这也将以一种巧妙的方式处理 Controller 转发。
如果您了解 Java EE,这类似于容器管理的事务。
是否有针对此的 bundle (或其他不错的解决方案)?
最佳答案
由于我没有找到解决方案,所以我决定创建一个。
PluessDoctrineTrxBundle完全符合我的要求。您为您的操作添加注释,所有 Doctrine 操作都包含在一个事务中。
关于symfony - 在 symfony2 中寻找 Controller Action 的@transaction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14274219/