| 1 | <?php |
||
| 12 | final class TerminateEventListener implements EventSubscriberInterface |
||
| 13 | { |
||
| 14 | /** @var EntityManager */ |
||
| 15 | private $entityManager; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * TerminateEventListener constructor. |
||
| 19 | * |
||
| 20 | * @param EntityManager $entityManager |
||
| 21 | */ |
||
| 22 | public function __construct(EntityManager $entityManager) |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @return array |
||
| 29 | */ |
||
| 30 | public static function getSubscribedEvents(): array |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param PostResponseEvent $event |
||
| 39 | * |
||
| 40 | * @throws \Doctrine\Common\Persistence\Mapping\MappingException |
||
| 41 | */ |
||
| 42 | public function handle(PostResponseEvent $event): void |
||
| 46 | } |
||
| 47 |
The
EntityManagermight become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring this request will fail.On the other hand, if you instead inject the
ManagerRegistry, thegetManager()method guarantees that you will always get a usable manager instance.