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
|
|
|
* @param array $criteria |
202
|
|
|
* |
203
|
|
|
* @return int The quantity of objects that matches the criteria. |
204
|
|
|
*/ |
205
|
2 |
|
public function count(array $criteria) |
206
|
|
|
{ |
207
|
2 |
|
$persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName); |
208
|
|
|
|
209
|
2 |
|
return $persister->count($criteria); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Adds support for magic finders/counters. |
214
|
|
|
* |
215
|
|
|
* @param string $method |
216
|
|
|
* @param array $arguments |
217
|
|
|
* |
218
|
|
|
* @return array|object|int The found entity/entities or its resulting count. |
219
|
|
|
* |
220
|
|
|
* @throws ORMException |
221
|
|
|
* @throws \BadMethodCallException If the method called is an invalid find* or countBy method |
222
|
|
|
* or no find* or countBy method at all and therefore an invalid |
223
|
|
|
* method call. |
224
|
|
|
*/ |
225
|
13 |
|
public function __call($method, $arguments) |
226
|
|
|
{ |
227
|
|
|
switch (true) { |
228
|
13 |
|
case (0 === strpos($method, 'findBy')): |
229
|
8 |
|
$by = substr($method, 6); |
230
|
8 |
|
$method = 'findBy'; |
231
|
8 |
|
break; |
232
|
|
|
|
233
|
5 |
|
case (0 === strpos($method, 'findOneBy')): |
234
|
3 |
|
$by = substr($method, 9); |
235
|
3 |
|
$method = 'findOneBy'; |
236
|
3 |
|
break; |
237
|
|
|
|
238
|
2 |
|
case (0 === strpos($method, 'countBy')): |
239
|
1 |
|
$by = substr($method, 7); |
240
|
1 |
|
$method = 'count'; |
241
|
1 |
|
break; |
242
|
|
|
|
243
|
|
|
default: |
244
|
1 |
|
throw new \BadMethodCallException( |
245
|
1 |
|
"Undefined method '$method'. The method name must start with ". |
246
|
1 |
|
"either findBy or findOneBy!" |
247
|
|
|
); |
248
|
|
|
} |
249
|
|
|
|
250
|
12 |
|
$argsCount = count($arguments); |
251
|
|
|
|
252
|
12 |
|
if (0 === $argsCount) { |
253
|
1 |
|
throw ORMException::findByRequiresParameter($method . $by); |
254
|
|
|
} |
255
|
|
|
|
256
|
11 |
|
$fieldName = lcfirst(\Doctrine\Common\Util\Inflector::classify($by)); |
257
|
|
|
|
258
|
11 |
|
if ($this->_class->hasField($fieldName) || $this->_class->hasAssociation($fieldName)) { |
259
|
|
|
switch ($argsCount) { |
260
|
10 |
|
case 1: |
261
|
8 |
|
return $this->$method(array($fieldName => $arguments[0])); |
262
|
|
|
|
263
|
2 |
|
case 2: |
264
|
1 |
|
return $this->$method(array($fieldName => $arguments[0]), $arguments[1]); |
265
|
|
|
|
266
|
1 |
|
case 3: |
267
|
|
|
return $this->$method(array($fieldName => $arguments[0]), $arguments[1], $arguments[2]); |
268
|
|
|
|
269
|
1 |
|
case 4: |
270
|
1 |
|
return $this->$method(array($fieldName => $arguments[0]), $arguments[1], $arguments[2], $arguments[3]); |
271
|
|
|
|
272
|
|
|
default: |
273
|
|
|
// Do nothing |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
277
|
1 |
|
throw ORMException::invalidFindByCall($this->_entityName, $fieldName, $method.$by); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @return string |
282
|
|
|
*/ |
283
|
|
|
protected function getEntityName() |
284
|
|
|
{ |
285
|
|
|
return $this->_entityName; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @return string |
290
|
|
|
*/ |
291
|
|
|
public function getClassName() |
292
|
|
|
{ |
293
|
|
|
return $this->getEntityName(); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @return EntityManager |
298
|
|
|
*/ |
299
|
|
|
protected function getEntityManager() |
300
|
|
|
{ |
301
|
|
|
return $this->_em; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @return Mapping\ClassMetadata |
306
|
|
|
*/ |
307
|
|
|
protected function getClassMetadata() |
308
|
|
|
{ |
309
|
|
|
return $this->_class; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Select all elements from a selectable that match the expression and |
314
|
|
|
* return a new collection containing these elements. |
315
|
|
|
* |
316
|
|
|
* @param \Doctrine\Common\Collections\Criteria $criteria |
317
|
|
|
* |
318
|
|
|
* @return \Doctrine\Common\Collections\Collection |
319
|
|
|
*/ |
320
|
24 |
|
public function matching(Criteria $criteria) |
321
|
|
|
{ |
322
|
24 |
|
$persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName); |
323
|
|
|
|
324
|
24 |
|
return new LazyCriteriaCollection($persister, $criteria); |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|
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.