[Doctrine\\ORM\\ORMException]
The EntityManager is closed.
After I get a DBAL exception when inserting data, EntityManager closes and I\'m not
Symfony v4.1.6
Doctrine v2.9.0
Process inserting duplicates in a repository
//begin of repo
/** @var RegistryInterface */
protected $registry;
public function __construct(RegistryInterface $registry)
{
$this->registry = $registry;
parent::__construct($registry, YourEntity::class);
}
//in repo method
$em = $this->getEntityManager();
$em->beginTransaction();
try {
$em->persist($yourEntityThatCanBeDuplicate);
$em->flush();
$em->commit();
} catch (\Throwable $e) {
//Rollback all nested transactions
while ($em->getConnection()->getTransactionNestingLevel() > 0) {
$em->rollback();
}
//Reset the default em
if (!$em->isOpen()) {
$this->registry->resetManager();
}
}