1 | <?php |
||
19 | abstract class AbstractORMRepository implements Repository |
||
20 | { |
||
21 | /** |
||
22 | * @var EntityManager |
||
23 | */ |
||
24 | protected $em; |
||
25 | /** |
||
26 | * @var EntityRepository |
||
27 | */ |
||
28 | protected $entityRepository; |
||
29 | |||
30 | public function __construct(EntityManager $em) |
||
|
|||
31 | { |
||
32 | $this->em = $em; |
||
33 | $this->entityRepository = $em->getRepository($this->getClassName()); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @return AggregateRoot[] |
||
38 | */ |
||
39 | public function getAll(): array |
||
40 | { |
||
41 | return $this->entityRepository->findAll(); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @param Identity $identity |
||
46 | * |
||
47 | * @return AggregateRoot |
||
48 | */ |
||
49 | public function get(Identity $identity) |
||
50 | { |
||
51 | return $this->entityRepository->find($identity); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @param AggregateRoot $document |
||
56 | */ |
||
57 | public function add(AggregateRoot $document) |
||
61 | |||
62 | /** |
||
63 | * Finds Entities by a set of criteria. |
||
64 | * |
||
65 | * @param array $criteria Query criteria |
||
66 | * @param array $sort Sort array for Cursor::sort() |
||
67 | * @param int|null $limit Limit for Cursor::limit() |
||
68 | * @param int|null $skip Skip for Cursor::skip() |
||
69 | * |
||
70 | * @return AggregateRoot[] |
||
71 | */ |
||
72 | protected function getBy(array $criteria, array $sort = null, int $limit = null, int $skip = null) |
||
76 | |||
77 | /** |
||
78 | * Finds a single AggregateRoot by a set of criteria. |
||
79 | * |
||
80 | * @param array $criteria |
||
81 | * |
||
82 | * @return AggregateRoot |
||
83 | */ |
||
84 | protected function getOneBy(array $criteria) |
||
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | protected function applyCriteria(QueryBuilder $queryBuilder, array $criteria = [], array $rootProperties = []) |
||
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | protected function applySorting(QueryBuilder $queryBuilder, array $sorting = null, array $rootProperties = []) |
||
134 | |||
135 | /** |
||
136 | * @param string $name |
||
137 | * @param array $rootProperties Properties that will not be bound to the object alias |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | protected function getPropertyName(string $name, array $rootProperties = []): string |
||
149 | |||
150 | /** |
||
151 | * If properties have complex path, it cannot be used as parameter key in |
||
152 | * requests. |
||
153 | * This sanitize property path for proper requests. |
||
154 | * |
||
155 | * @param string $property |
||
156 | * |
||
157 | * @return string |
||
158 | */ |
||
159 | protected function sanitizeParameterName(string $property): string |
||
163 | |||
164 | /** |
||
165 | * @return string The main alias used for this repository's entity in queries. |
||
166 | */ |
||
167 | protected function getAlias(): string |
||
175 | } |
||
176 |
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.