1 | <?php namespace Nord\Lumen\Doctrine\ORM\Console; |
||
6 | abstract class DoctrineCommand extends Command |
||
7 | { |
||
8 | |||
9 | /** |
||
10 | * @var EntityManager |
||
11 | */ |
||
12 | private $entityManager; |
||
13 | |||
14 | |||
15 | /** |
||
16 | * DoctrineCommand constructor. |
||
17 | * |
||
18 | * @param EntityManager $entityManager |
||
19 | */ |
||
20 | public function __construct(EntityManager $entityManager) |
||
26 | |||
27 | |||
28 | /** |
||
29 | * @return EntityManager |
||
30 | */ |
||
31 | protected function getEntityManager() |
||
35 | } |
||
36 |
The
EntityManager
might 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
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during 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.