1 | <?php |
||
13 | class EntityID |
||
14 | { |
||
15 | /** |
||
16 | * @var EntityManager |
||
17 | */ |
||
18 | private $entityManager; |
||
19 | |||
20 | /** |
||
21 | * The entity class name |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | private $className; |
||
26 | |||
27 | /** |
||
28 | * The entity id |
||
29 | * |
||
30 | * @var null|string |
||
31 | */ |
||
32 | private $id; |
||
33 | |||
34 | 4 | public function __construct(EntityManager $entityManager, string $className, ?string $id) |
|
40 | |||
41 | /** |
||
42 | * Get the entity from DB |
||
43 | * |
||
44 | * @return mixed entity |
||
45 | */ |
||
46 | 4 | public function getEntity() |
|
55 | } |
||
56 |
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.