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