The EntityManager is closed

前端 未结 17 1779
星月不相逢
星月不相逢 2020-11-29 18:36
[Doctrine\\ORM\\ORMException]   
The EntityManager is closed.  

After I get a DBAL exception when inserting data, EntityManager closes and I\'m not

17条回答
  •  一个人的身影
    2020-11-29 18:55

    In Symfony 4.2+ you have to use the package:

    composer require symfony/proxy-manager-bridge
    

    otherwiese you get the exception:

    Resetting a non-lazy manager service is not supported. Declare the "doctrine.orm.default_entity_manager" service as lazy.  
    

    Than you can reset the entityManager like this:

    services.yaml:

    App\Foo:
        - '@doctrine.orm.entity_manager'
        - '@doctrine'
    

    Foo.php:

    use Doctrine\Bundle\DoctrineBundle\Registry;
    use Doctrine\DBAL\DBALException;
    use Doctrine\ORM\EntityManagerInterface;
    
    
     try {
        $this->entityManager->persist($entity);
        $this->entityManager->flush();
    } catch (DBALException $e) {
        if (!$this->entityManager->isOpen()) {
            $this->entityManager = $this->doctrine->resetManager();
        }
    }
    

提交回复
热议问题