1 | <?php |
||
14 | class EntityIDType extends IDType |
||
15 | { |
||
16 | /** |
||
17 | * @var EntityManager |
||
18 | */ |
||
19 | private $entityManager; |
||
20 | |||
21 | /** |
||
22 | * The entity class name |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | private $className; |
||
27 | |||
28 | 9 | 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 | * |
||
43 | * @return string |
||
44 | */ |
||
45 | 1 | public function serialize($value) |
|
51 | |||
52 | /** |
||
53 | * Parses an externally provided value (query variable) to use as an input |
||
54 | * |
||
55 | * @param mixed $value |
||
56 | * |
||
57 | * @return mixed A Doctrine entity |
||
58 | */ |
||
59 | 2 | public function parseValue($value) |
|
65 | |||
66 | /** |
||
67 | * Parses an externally provided literal value (hardcoded in GraphQL query) to use as an input |
||
68 | * |
||
69 | * @param \GraphQL\Language\AST\Node $valueNode |
||
70 | * |
||
71 | * @return mixed |
||
72 | */ |
||
73 | 2 | public function parseLiteral($valueNode) |
|
79 | |||
80 | /** |
||
81 | * Create EntityID to retrieve entity from DB later on |
||
82 | * |
||
83 | * @return mixed entity |
||
84 | */ |
||
85 | 4 | private function createEntityID(string $id) |
|
89 | } |
||
90 |
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.