1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\ORM; |
21
|
|
|
|
22
|
|
|
use Doctrine\ORM\Query\ResultSetMappingBuilder; |
23
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
24
|
|
|
use Doctrine\Common\Collections\Selectable; |
25
|
|
|
use Doctrine\Common\Collections\Criteria; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* An EntityRepository serves as a repository for entities with generic as well as |
29
|
|
|
* business specific methods for retrieving entities. |
30
|
|
|
* |
31
|
|
|
* This class is designed for inheritance and users can subclass this class to |
32
|
|
|
* write their own repositories with business-specific methods to locate entities. |
33
|
|
|
* |
34
|
|
|
* @since 2.0 |
35
|
|
|
* @author Benjamin Eberlei <[email protected]> |
36
|
|
|
* @author Guilherme Blanco <[email protected]> |
37
|
|
|
* @author Jonathan Wage <[email protected]> |
38
|
|
|
* @author Roman Borschel <[email protected]> |
39
|
|
|
*/ |
40
|
|
|
class EntityRepository implements ObjectRepository, Selectable |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $_entityName; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var EntityManager |
49
|
|
|
*/ |
50
|
|
|
protected $_em; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var \Doctrine\ORM\Mapping\ClassMetadata |
54
|
|
|
*/ |
55
|
|
|
protected $_class; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Initializes a new <tt>EntityRepository</tt>. |
59
|
|
|
* |
60
|
|
|
* @param EntityManager $em The EntityManager to use. |
61
|
|
|
* @param Mapping\ClassMetadata $class The class descriptor. |
62
|
|
|
*/ |
63
|
146 |
|
public function __construct(EntityManagerInterface $em, Mapping\ClassMetadata $class) |
64
|
|
|
{ |
65
|
146 |
|
$this->_entityName = $class->name; |
66
|
146 |
|
$this->_em = $em; |
|
|
|
|
67
|
146 |
|
$this->_class = $class; |
68
|
146 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Creates a new QueryBuilder instance that is prepopulated for this entity name. |
72
|
|
|
* |
73
|
|
|
* @param string $alias |
74
|
|
|
* @param string $indexBy The index for the from. |
75
|
|
|
* |
76
|
|
|
* @return QueryBuilder |
77
|
|
|
*/ |
78
|
6 |
|
public function createQueryBuilder($alias, $indexBy = null) |
79
|
|
|
{ |
80
|
6 |
|
return $this->_em->createQueryBuilder() |
81
|
6 |
|
->select($alias) |
82
|
6 |
|
->from($this->_entityName, $alias, $indexBy); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Creates a new result set mapping builder for this entity. |
87
|
|
|
* |
88
|
|
|
* The column naming strategy is "INCREMENT". |
89
|
|
|
* |
90
|
|
|
* @param string $alias |
91
|
|
|
* |
92
|
|
|
* @return ResultSetMappingBuilder |
93
|
|
|
*/ |
94
|
1 |
|
public function createResultSetMappingBuilder($alias) |
95
|
|
|
{ |
96
|
1 |
|
$rsm = new ResultSetMappingBuilder($this->_em, ResultSetMappingBuilder::COLUMN_RENAMING_INCREMENT); |
97
|
1 |
|
$rsm->addRootEntityFromClassMetadata($this->_entityName, $alias); |
98
|
|
|
|
99
|
1 |
|
return $rsm; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Creates a new Query instance based on a predefined metadata named query. |
104
|
|
|
* |
105
|
|
|
* @param string $queryName |
106
|
|
|
* |
107
|
|
|
* @return Query |
108
|
|
|
*/ |
109
|
3 |
|
public function createNamedQuery($queryName) |
110
|
|
|
{ |
111
|
3 |
|
return $this->_em->createQuery($this->_class->getNamedQuery($queryName)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Creates a native SQL query. |
116
|
|
|
* |
117
|
|
|
* @param string $queryName |
118
|
|
|
* |
119
|
|
|
* @return NativeQuery |
120
|
|
|
*/ |
121
|
7 |
|
public function createNativeNamedQuery($queryName) |
122
|
|
|
{ |
123
|
7 |
|
$queryMapping = $this->_class->getNamedNativeQuery($queryName); |
124
|
7 |
|
$rsm = new Query\ResultSetMappingBuilder($this->_em); |
125
|
7 |
|
$rsm->addNamedNativeQueryMapping($this->_class, $queryMapping); |
126
|
|
|
|
127
|
7 |
|
return $this->_em->createNativeQuery($queryMapping['query'], $rsm); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Clears the repository, causing all managed entities to become detached. |
132
|
|
|
* |
133
|
|
|
* @return void |
134
|
|
|
*/ |
135
|
|
|
public function clear() |
136
|
|
|
{ |
137
|
|
|
$this->_em->clear($this->_class->rootEntityName); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Finds an entity by its primary key / identifier. |
142
|
|
|
* |
143
|
|
|
* @param mixed $id The identifier. |
144
|
|
|
* @param int|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants |
145
|
|
|
* or NULL if no specific lock mode should be used |
146
|
|
|
* during the search. |
147
|
|
|
* @param int|null $lockVersion The lock version. |
148
|
|
|
* |
149
|
|
|
* @return object|null The entity instance or NULL if the entity can not be found. |
150
|
|
|
*/ |
151
|
14 |
|
public function find($id, $lockMode = null, $lockVersion = null) |
152
|
|
|
{ |
153
|
14 |
|
return $this->_em->find($this->_entityName, $id, $lockMode, $lockVersion); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Finds all entities in the repository. |
158
|
|
|
* |
159
|
|
|
* @return array The entities. |
160
|
|
|
*/ |
161
|
30 |
|
public function findAll() |
162
|
|
|
{ |
163
|
30 |
|
return $this->findBy(array()); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Finds entities by a set of criteria. |
168
|
|
|
* |
169
|
|
|
* @param array $criteria |
170
|
|
|
* @param array|null $orderBy |
171
|
|
|
* @param int|null $limit |
172
|
|
|
* @param int|null $offset |
173
|
|
|
* |
174
|
|
|
* @return array The objects. |
175
|
|
|
*/ |
176
|
60 |
|
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
177
|
|
|
{ |
178
|
60 |
|
$persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName); |
179
|
|
|
|
180
|
60 |
|
return $persister->loadAll($criteria, $orderBy, $limit, $offset); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Finds a single entity by a set of criteria. |
185
|
|
|
* |
186
|
|
|
* @param array $criteria |
187
|
|
|
* @param array|null $orderBy |
188
|
|
|
* |
189
|
|
|
* @return object|null The entity instance or NULL if the entity can not be found. |
190
|
|
|
*/ |
191
|
21 |
|
public function findOneBy(array $criteria, array $orderBy = null) |
192
|
|
|
{ |
193
|
21 |
|
$persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName); |
194
|
|
|
|
195
|
21 |
|
return $persister->load($criteria, null, null, array(), null, 1, $orderBy); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Counts entities by a set of criteria. |
200
|
|
|
* |
201
|
|
|
* @todo Add this method to `ObjectRepository` interface in the next major release |
202
|
|
|
* |
203
|
|
|
* @param array $criteria |
204
|
|
|
* |
205
|
|
|
* @return int The quantity of objects that matches the criteria. |
206
|
|
|
*/ |
207
|
2 |
|
public function count(array $criteria) |
208
|
|
|
{ |
209
|
2 |
|
$persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName); |
210
|
|
|
|
211
|
2 |
|
return $persister->count($criteria); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Adds support for magic method calls. |
216
|
|
|
* |
217
|
|
|
* @param string $method |
218
|
|
|
* @param array $arguments |
219
|
|
|
* |
220
|
|
|
* @return mixed The returned value from the resolved method. |
221
|
|
|
* |
222
|
|
|
* @throws ORMException |
223
|
|
|
* @throws \BadMethodCallException If the method called is invalid |
224
|
|
|
*/ |
225
|
13 |
|
public function __call($method, $arguments) |
226
|
|
|
{ |
227
|
13 |
|
if (0 === strpos($method, 'findBy')) { |
228
|
8 |
|
return $this->resolveMagicCall('findBy', substr($method, 6), $arguments); |
229
|
|
|
} |
230
|
|
|
|
231
|
5 |
|
if (0 === strpos($method, 'findOneBy')) { |
232
|
3 |
|
return $this->resolveMagicCall('findOneBy', substr($method, 9), $arguments); |
233
|
|
|
} |
234
|
|
|
|
235
|
2 |
|
if (0 === strpos($method, 'countBy')) { |
236
|
1 |
|
return $this->resolveMagicCall('count', substr($method, 7), $arguments); |
237
|
|
|
} |
238
|
|
|
|
239
|
1 |
|
throw new \BadMethodCallException( |
240
|
1 |
|
"Undefined method '$method'. The method name must start with ". |
241
|
1 |
|
"either findBy or findOneBy!" |
242
|
|
|
); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @return string |
247
|
|
|
*/ |
248
|
|
|
protected function getEntityName() |
249
|
|
|
{ |
250
|
|
|
return $this->_entityName; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @return string |
255
|
|
|
*/ |
256
|
|
|
public function getClassName() |
257
|
|
|
{ |
258
|
|
|
return $this->getEntityName(); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @return EntityManager |
263
|
|
|
*/ |
264
|
|
|
protected function getEntityManager() |
265
|
|
|
{ |
266
|
|
|
return $this->_em; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @return Mapping\ClassMetadata |
271
|
|
|
*/ |
272
|
|
|
protected function getClassMetadata() |
273
|
|
|
{ |
274
|
|
|
return $this->_class; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Select all elements from a selectable that match the expression and |
279
|
|
|
* return a new collection containing these elements. |
280
|
|
|
* |
281
|
|
|
* @param \Doctrine\Common\Collections\Criteria $criteria |
282
|
|
|
* |
283
|
|
|
* @return \Doctrine\Common\Collections\Collection |
284
|
|
|
*/ |
285
|
24 |
|
public function matching(Criteria $criteria) |
286
|
|
|
{ |
287
|
24 |
|
$persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName); |
288
|
|
|
|
289
|
24 |
|
return new LazyCriteriaCollection($persister, $criteria); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Resolves a magic method call to the proper existent method at `EntityRepository`. |
294
|
|
|
* |
295
|
|
|
* @param string $method The method to call |
296
|
|
|
* @param string $by The property name used as condition |
297
|
|
|
* @param array $arguments The arguments to pass at method call |
298
|
|
|
* |
299
|
|
|
* @throws ORMException If the method called is invalid. |
300
|
|
|
* |
301
|
|
|
* @return mixed |
302
|
|
|
*/ |
303
|
12 |
|
private function resolveMagicCall($method, $by, array $arguments = []) |
304
|
|
|
{ |
305
|
12 |
|
$argsCount = count($arguments); |
306
|
|
|
|
307
|
12 |
|
if (0 === $argsCount) { |
308
|
1 |
|
throw ORMException::findByRequiresParameter($method . $by); |
309
|
|
|
} |
310
|
|
|
|
311
|
11 |
|
$fieldName = lcfirst(\Doctrine\Common\Util\Inflector::classify($by)); |
312
|
|
|
|
313
|
11 |
|
if ($this->_class->hasField($fieldName) || $this->_class->hasAssociation($fieldName)) { |
314
|
|
|
switch ($argsCount) { |
315
|
10 |
|
case 1: |
316
|
8 |
|
return $this->$method(array($fieldName => $arguments[0])); |
317
|
|
|
|
318
|
2 |
|
case 2: |
319
|
1 |
|
return $this->$method(array($fieldName => $arguments[0]), $arguments[1]); |
320
|
|
|
|
321
|
1 |
|
case 3: |
322
|
|
|
return $this->$method(array($fieldName => $arguments[0]), $arguments[1], $arguments[2]); |
323
|
|
|
|
324
|
1 |
|
case 4: |
325
|
1 |
|
return $this->$method(array($fieldName => $arguments[0]), $arguments[1], $arguments[2], $arguments[3]); |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|
329
|
1 |
|
throw ORMException::invalidMagicCall($this->_entityName, $fieldName, $method.$by); |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.