【问题标题】:Looking for @transaction for controller actions in symfony2为 symfony2 中的控制器操作寻找 @transaction
【发布时间】:2012-12-25 19:15:25
【问题描述】:

为了确保我的操作能够正确处理事务,我发现自己在我的控制器中一遍又一遍地重复此代码:

/**
 * @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 之类的东西。这将进入操作的注释并为我节省大量重复的代码。在一个完美的世界中,这也可以巧妙地处理控制器转发。

如果您了解 Java EE,这将类似于容器管理的事务。

是否有适用于此的捆绑包(或其他不错的解决方案)?

【问题讨论】:

    标签: symfony transactions controller doctrine


    【解决方案1】:

    由于我没有找到解决方案,我决定创建一个。

    PluessDoctrineTrxBundle 完全符合我的要求。您为您的操作添加注释,并且所有条令操作都包含在事务中。

    【讨论】:

      【解决方案2】:

      你可以使用 JMSAopBundle 在 Symfony2 中实现一个带有 AOP 的包装器。步骤在此链接中详细说明:

      http://jmsyst.com/bundles/JMSAopBundle#transaction-management

      在示例中,作者获取了一个名为 @Transactional 的注解,类似于 nameshake Spring 的注解:

      http://docs.spring.io/spring-framework/docs/4.2.x/spring-framework-reference/html/transaction.html#transaction-declarative-annotations

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多