Zend framework2 zend framework 2与条令,如何在Entitymanager被异常关闭后重新打开它

Zend framework2 zend framework 2与条令,如何在Entitymanager被异常关闭后重新打开它,zend-framework2,zfdoctrine,Zend Framework2,Zfdoctrine,在实体的存储库中,我希望获取具有给定id的实体,或者创建一个新实体(如果到目前为止尚未使用给定id)。我的解决方案是,创建一个id为的新实体。如果可能,我将返回它,如果不可能,我将加载激活的实体。非常糟糕,这是不可能的,因为entitymanager已关闭 class TestRepository extends Repository { // create a new entity or load the existing one public function getEnt

在实体的存储库中,我希望获取具有给定id的实体,或者创建一个新实体(如果到目前为止尚未使用给定id)。我的解决方案是,创建一个id为的新实体。如果可能,我将返回它,如果不可能,我将加载激活的实体。非常糟糕,这是不可能的,因为entitymanager已关闭

class TestRepository extends Repository {

    // create a new entity or load the existing one
    public function getEntity($pkey) {
        $entity = new Entity($pkey); // create a new entity and set its id to $pkey
        $this->getEntityManager()->persist($language);
        try {
            $this->getEntityManager()->flush(); // success if $pkey isn't used
        } catch (DBALException $e) {
            // this DBALException is catched correct
            // fail - load the existing one
            $entity = $this->find(array($pkey)); // another exception is thrown
            // The EntityManager is closed.
        }
        return $entity;
    }

}

如何重新打开EntityManger?

为什么要尝试使用已定义的主键持久化实体,而您不知道该主键是否存在

最好先创建一个简单的find(),如果它不返回任何结果,则创建实体并将其持久化


希望这有帮助。

对不起,我看到有一些拼写错误:$language表示$entity,find(array($pkey))表示find($pkey)。