Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

Many reasons might cause this error but the solution is simple.


Error


Doctrine\ORM\ORMException exception "The EntityManager is closed".

The most common causes



Example solution


public function doSoemthing()
{
// ....
$this->em = $this->refreshEntityManager($this->em);
// ....
}

/**
* @param $em EntityManager
*
* @return EntityManager
*/
private function refreshEntityManager(EntityManager $em)
{
if (!$em->isOpen()) {
$em = $em->create($em->getConnection(), $em->getConfiguration());
}

return $em;
}