使用find from repository抛出TransactionRequiredException

时间:2014-03-23 15:03:52

标签: symfony doctrine-orm doctrine

下一个代码是抛出 TransactionRequiredException

$entity = $this
            ->getDoctrine()
            ->getRepository('MyBundle:MyEntity')
            ->find($id);

下一个工作正常:

$entity = $this
            ->getDoctrine()
            ->getManager()
            ->find('MyBundle:MyEntity', $id);

这些是我的作曲家依赖:

"php"                                 : ">=5.5",
"symfony/symfony"                     : "~2.4",
"doctrine/orm"                        : "~2.2,>=2.2.3",
"doctrine/doctrine-bundle"            : "~1.2",
"doctrine/doctrine-fixtures-bundle"   : "~2.2",
"twig/extensions"                     : "~1.0",
"symfony/assetic-bundle"              : "~2.3",
"symfony/swiftmailer-bundle"          : "~2.3",
"symfony/monolog-bundle"              : "~2.4",
"sensio/distribution-bundle"          : "~2.3",
"sensio/framework-extra-bundle"       : "~2.3",
"sensio/generator-bundle"             : "~2.3",
"incenteev/composer-parameter-handler": "~2.0",
"friendsofsymfony/user-bundle"        : "~2.0@dev",
"stof/doctrine-extensions-bundle"     : "~1.1",
"knplabs/knp-menu-bundle"             : "~2.0",
"knplabs/knp-snappy-bundle"           : "~1.2",
"white-october/pagerfanta-bundle"     : "~1.0",
"jms/di-extra-bundle"                 : "~1.4",
"lexik/form-filter-bundle"            : "~2.0"

该实体正在按照 MappedSuperclass 策略扩展一个抽象的实体,并且有一些 OneToMany ManyToMany 关系。

我真的不知道为什么会发生这种情况只是想"阅读"。

1 个答案:

答案 0 :(得分:1)

好的,我发现这个问题只是调试了一下......

MyEntityRepository 覆盖了查找方法,如:

public function find($id, $lockMode = LockMode::NONE, $lockVersion = null)
{
    return parent::find($id, $lockMode, $lockVersion);
}

但是,如果this revision我们应该将$lockMode设置为null ,如果在搜索期间不应使用特定的锁定模式