The EntityManager is closed

Ueli picture Ueli · Jan 10, 2013 · Viewed 105.2k times · Source
[Doctrine\ORM\ORMException]   
The EntityManager is closed.  

After I get a DBAL exception when inserting data, EntityManager closes and I'm not able to reconnect it.

I tried like this but it didn't get a connection.

$this->em->close();
$this->set('doctrine.orm.entity_manager', null);
$this->set('doctrine.orm.default_entity_manager', null);
$this->get('doctrine')->resetEntityManager();
$this->em = $this->get('doctrine')->getEntityManager();

Anyone an idea how to reconnect?

Answer

Gregsparrow picture Gregsparrow · Sep 29, 2013

My solution.

Before doing anything check:

if (!$this->entityManager->isOpen()) {
    $this->entityManager = $this->entityManager->create(
        $this->entityManager->getConnection(),
        $this->entityManager->getConfiguration()
    );
}

All entities will be saved. But it is handy for particular class or some cases. If you have some services with injected entitymanager, it still be closed.