Reputation: 1
Often encountered the following piece of code:
catch (Exception $ex){
$em->clear();
if($em->getConnection()->isTransactionActive())
$em->rollback();
{somecode}
}
First thought - create inheritor of EntityManager,contains method, implementing clear and rollback, and put it to DI container. But Doctrine EntityManager class in comment mark as final:
/* final */class EntityManager implements EntityManagerInterface
Helper as service will be ugly. Any idea?
Upvotes: 0
Views: 44
Reputation: 17166
You would use decoration instead. It will roughly look like this:
class MyEntityManager implements EntityManagerInterface
{
private $decoratedManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->decoratedManager = $entityManager;
}
/**
* Implement all methods on the interface like this:
*/
public function persist($entity)
{
return $this->decoratedManager->persist($entity);
}
}
This will not break final and you can easily override functions. Then in your service configuration you have to overwrite the service definition for the entity manager or make sure that whenever you want to inject an EntityManagerInterface
into a service your's is picked.
Upvotes: 1