symfony - 在 symfony2 中寻找 Controller Action 的@transaction

标签 symfony transactions controller doctrine

为了确保我的操作能够正确处理事务,我发现自己在我的 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/

相关文章:

mysql - MySQL 中的 SELECT FOR UPDATE 有多安全

javascript - yii2 从 View 和调试进程中调用 Controller 操作

ruby-on-rails - 如何将设计身份验证详细信息传递给 Rails Controller 操作?

php - undefined variable : data (View: C:\cygwin64\home\hp\AddressBook\resources\views\Update\edit.blade.php)

symfony - 使用 Mink 测试时模拟服务?

transactions - 交易(技术上)如何运作?

自治事务中的 Oracle DDL

symfony - Symfony2电子商务捆绑了反馈

php - 使用闭包表模型的 Doctrine2 分层数据

php - allow_if 语句中不可解析的 IP 列表