1 | <?php |
||
15 | class EntityIDType extends IDType |
||
16 | { |
||
17 | /** |
||
18 | * @var EntityManager |
||
19 | */ |
||
20 | private $entityManager; |
||
21 | |||
22 | /** |
||
23 | * The entity class name |
||
24 | * @var string |
||
25 | */ |
||
26 | private $className; |
||
27 | |||
28 | 4 | public function __construct(EntityManager $entityManager, string $className) |
|
37 | |||
38 | /** |
||
39 | * Serializes an internal value to include in a response. |
||
40 | * |
||
41 | * @param mixed $value |
||
42 | * @return mixed |
||
43 | */ |
||
44 | 1 | public function serialize($value) |
|
50 | |||
51 | /** |
||
52 | * Parses an externally provided value (query variable) to use as an input |
||
53 | * |
||
54 | * @param mixed $value |
||
55 | * @return mixed A Doctrine entity |
||
56 | */ |
||
57 | 1 | public function parseValue($value) |
|
63 | |||
64 | /** |
||
65 | * Get the repository for our entity |
||
66 | * @return EntityRepository |
||
67 | */ |
||
68 | 1 | private function getRepository(): EntityRepository |
|
72 | } |
||
73 |
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.