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 Exception; |
23
|
|
|
use Doctrine\Common\EventManager; |
24
|
|
|
use Doctrine\DBAL\Connection; |
25
|
|
|
use Doctrine\DBAL\DriverManager; |
26
|
|
|
use Doctrine\DBAL\LockMode; |
27
|
|
|
use Doctrine\ORM\Query\ResultSetMapping; |
28
|
|
|
use Doctrine\ORM\Proxy\ProxyFactory; |
29
|
|
|
use Doctrine\ORM\Query\FilterCollection; |
30
|
|
|
use Doctrine\Common\Util\ClassUtils; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The EntityManager is the central access point to ORM functionality. |
34
|
|
|
* |
35
|
|
|
* It is a facade to all different ORM subsystems such as UnitOfWork, |
36
|
|
|
* Query Language and Repository API. Instantiation is done through |
37
|
|
|
* the static create() method. The quickest way to obtain a fully |
38
|
|
|
* configured EntityManager is: |
39
|
|
|
* |
40
|
|
|
* use Doctrine\ORM\Tools\Setup; |
41
|
|
|
* use Doctrine\ORM\EntityManager; |
42
|
|
|
* |
43
|
|
|
* $paths = array('/path/to/entity/mapping/files'); |
44
|
|
|
* |
45
|
|
|
* $config = Setup::createAnnotationMetadataConfiguration($paths); |
46
|
|
|
* $dbParams = array('driver' => 'pdo_sqlite', 'memory' => true); |
47
|
|
|
* $entityManager = EntityManager::create($dbParams, $config); |
48
|
|
|
* |
49
|
|
|
* For more information see |
50
|
|
|
* {@link http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/configuration.html} |
51
|
|
|
* |
52
|
|
|
* You should never attempt to inherit from the EntityManager: Inheritance |
53
|
|
|
* is not a valid extension point for the EntityManager. Instead you |
54
|
|
|
* should take a look at the {@see \Doctrine\ORM\Decorator\EntityManagerDecorator} |
55
|
|
|
* and wrap your entity manager in a decorator. |
56
|
|
|
* |
57
|
|
|
* @since 2.0 |
58
|
|
|
* @author Benjamin Eberlei <[email protected]> |
59
|
|
|
* @author Guilherme Blanco <[email protected]> |
60
|
|
|
* @author Jonathan Wage <[email protected]> |
61
|
|
|
* @author Roman Borschel <[email protected]> |
62
|
|
|
*/ |
63
|
|
|
/* final */class EntityManager implements EntityManagerInterface |
64
|
|
|
{ |
65
|
|
|
/** |
66
|
|
|
* The used Configuration. |
67
|
|
|
* |
68
|
|
|
* @var \Doctrine\ORM\Configuration |
69
|
|
|
*/ |
70
|
|
|
private $config; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* The database connection used by the EntityManager. |
74
|
|
|
* |
75
|
|
|
* @var \Doctrine\DBAL\Connection |
76
|
|
|
*/ |
77
|
|
|
private $conn; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* The metadata factory, used to retrieve the ORM metadata of entity classes. |
81
|
|
|
* |
82
|
|
|
* @var \Doctrine\ORM\Mapping\ClassMetadataFactory |
83
|
|
|
*/ |
84
|
|
|
private $metadataFactory; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* The UnitOfWork used to coordinate object-level transactions. |
88
|
|
|
* |
89
|
|
|
* @var \Doctrine\ORM\UnitOfWork |
90
|
|
|
*/ |
91
|
|
|
private $unitOfWork; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* The event manager that is the central point of the event system. |
95
|
|
|
* |
96
|
|
|
* @var \Doctrine\Common\EventManager |
97
|
|
|
*/ |
98
|
|
|
private $eventManager; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* The proxy factory used to create dynamic proxies. |
102
|
|
|
* |
103
|
|
|
* @var \Doctrine\ORM\Proxy\ProxyFactory |
104
|
|
|
*/ |
105
|
|
|
private $proxyFactory; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* The repository factory used to create dynamic repositories. |
109
|
|
|
* |
110
|
|
|
* @var \Doctrine\ORM\Repository\RepositoryFactory |
111
|
|
|
*/ |
112
|
|
|
private $repositoryFactory; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* The expression builder instance used to generate query expressions. |
116
|
|
|
* |
117
|
|
|
* @var \Doctrine\ORM\Query\Expr |
118
|
|
|
*/ |
119
|
|
|
private $expressionBuilder; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Whether the EntityManager is closed or not. |
123
|
|
|
* |
124
|
|
|
* @var bool |
125
|
|
|
*/ |
126
|
|
|
private $closed = false; |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Collection of query filters. |
130
|
|
|
* |
131
|
|
|
* @var \Doctrine\ORM\Query\FilterCollection |
132
|
|
|
*/ |
133
|
|
|
private $filterCollection; |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @var \Doctrine\ORM\Cache The second level cache regions API. |
137
|
|
|
*/ |
138
|
|
|
private $cache; |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Creates a new EntityManager that operates on the given database connection |
142
|
|
|
* and uses the given Configuration and EventManager implementations. |
143
|
|
|
* |
144
|
|
|
* @param \Doctrine\DBAL\Connection $conn |
145
|
|
|
* @param \Doctrine\ORM\Configuration $config |
146
|
|
|
* @param \Doctrine\Common\EventManager $eventManager |
147
|
|
|
*/ |
148
|
2347 |
|
protected function __construct(Connection $conn, Configuration $config, EventManager $eventManager) |
149
|
|
|
{ |
150
|
2347 |
|
$this->conn = $conn; |
151
|
2347 |
|
$this->config = $config; |
152
|
2347 |
|
$this->eventManager = $eventManager; |
153
|
|
|
|
154
|
2347 |
|
$metadataFactoryClassName = $config->getClassMetadataFactoryName(); |
155
|
|
|
|
156
|
2347 |
|
$this->metadataFactory = new $metadataFactoryClassName; |
157
|
2347 |
|
$this->metadataFactory->setEntityManager($this); |
158
|
2347 |
|
$this->metadataFactory->setCacheDriver($this->config->getMetadataCacheImpl()); |
159
|
|
|
|
160
|
2347 |
|
$this->repositoryFactory = $config->getRepositoryFactory(); |
161
|
2347 |
|
$this->unitOfWork = new UnitOfWork($this); |
162
|
2347 |
|
$this->proxyFactory = new ProxyFactory( |
163
|
|
|
$this, |
164
|
2347 |
|
$config->getProxyDir(), |
165
|
2347 |
|
$config->getProxyNamespace(), |
166
|
2347 |
|
$config->getAutoGenerateProxyClasses(), |
167
|
2347 |
|
$config->getProxyUmask() |
168
|
|
|
); |
169
|
|
|
|
170
|
2347 |
|
if ($config->isSecondLevelCacheEnabled()) { |
171
|
278 |
|
$cacheConfig = $config->getSecondLevelCacheConfiguration(); |
172
|
278 |
|
$cacheFactory = $cacheConfig->getCacheFactory(); |
173
|
278 |
|
$this->cache = $cacheFactory->createCache($this); |
174
|
|
|
} |
175
|
2347 |
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritDoc} |
179
|
|
|
*/ |
180
|
1839 |
|
public function getConnection() |
181
|
|
|
{ |
182
|
1839 |
|
return $this->conn; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Gets the metadata factory used to gather the metadata of classes. |
187
|
|
|
* |
188
|
|
|
* @return \Doctrine\ORM\Mapping\ClassMetadataFactory |
189
|
|
|
*/ |
190
|
2347 |
|
public function getMetadataFactory() |
191
|
|
|
{ |
192
|
2347 |
|
return $this->metadataFactory; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* {@inheritDoc} |
197
|
|
|
*/ |
198
|
17 |
|
public function getExpressionBuilder() |
199
|
|
|
{ |
200
|
17 |
|
if ($this->expressionBuilder === null) { |
201
|
17 |
|
$this->expressionBuilder = new Query\Expr; |
202
|
|
|
} |
203
|
|
|
|
204
|
17 |
|
return $this->expressionBuilder; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* {@inheritDoc} |
209
|
|
|
*/ |
210
|
2 |
|
public function beginTransaction() |
211
|
|
|
{ |
212
|
2 |
|
$this->conn->beginTransaction(); |
213
|
2 |
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* {@inheritDoc} |
217
|
|
|
*/ |
218
|
212 |
|
public function getCache() |
219
|
|
|
{ |
220
|
212 |
|
return $this->cache; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* {@inheritDoc} |
225
|
|
|
*/ |
226
|
4 |
|
public function transactional($func) |
227
|
|
|
{ |
228
|
4 |
|
if (!is_callable($func)) { |
229
|
1 |
|
throw new \InvalidArgumentException('Expected argument of type "callable", got "' . gettype($func) . '"'); |
230
|
|
|
} |
231
|
|
|
|
232
|
3 |
|
$this->conn->beginTransaction(); |
233
|
|
|
|
234
|
|
|
try { |
235
|
3 |
|
$return = call_user_func($func, $this); |
236
|
|
|
|
237
|
3 |
|
$this->flush(); |
238
|
3 |
|
$this->conn->commit(); |
239
|
|
|
|
240
|
3 |
|
return $return ?: true; |
241
|
|
|
} catch (Exception $e) { |
242
|
|
|
$this->close(); |
243
|
|
|
$this->conn->rollBack(); |
244
|
|
|
|
245
|
|
|
throw $e; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* {@inheritDoc} |
251
|
|
|
*/ |
252
|
1 |
|
public function commit() |
253
|
|
|
{ |
254
|
1 |
|
$this->conn->commit(); |
255
|
1 |
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* {@inheritDoc} |
259
|
|
|
*/ |
260
|
1 |
|
public function rollback() |
261
|
|
|
{ |
262
|
1 |
|
$this->conn->rollBack(); |
263
|
1 |
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Returns the ORM metadata descriptor for a class. |
267
|
|
|
* |
268
|
|
|
* The class name must be the fully-qualified class name without a leading backslash |
269
|
|
|
* (as it is returned by get_class($obj)) or an aliased class name. |
270
|
|
|
* |
271
|
|
|
* Examples: |
272
|
|
|
* MyProject\Domain\User |
273
|
|
|
* sales:PriceRequest |
274
|
|
|
* |
275
|
|
|
* Internal note: Performance-sensitive method. |
276
|
|
|
* |
277
|
|
|
* @param string $className |
278
|
|
|
* |
279
|
|
|
* @return \Doctrine\ORM\Mapping\ClassMetadata |
280
|
|
|
*/ |
281
|
1904 |
|
public function getClassMetadata($className) |
282
|
|
|
{ |
283
|
1904 |
|
return $this->metadataFactory->getMetadataFor($className); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* {@inheritDoc} |
288
|
|
|
*/ |
289
|
925 |
|
public function createQuery($dql = '') |
290
|
|
|
{ |
291
|
925 |
|
$query = new Query($this); |
292
|
|
|
|
293
|
925 |
|
if ( ! empty($dql)) { |
294
|
920 |
|
$query->setDQL($dql); |
295
|
|
|
} |
296
|
|
|
|
297
|
925 |
|
return $query; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* {@inheritDoc} |
302
|
|
|
*/ |
303
|
1 |
|
public function createNamedQuery($name) |
304
|
|
|
{ |
305
|
1 |
|
return $this->createQuery($this->config->getNamedQuery($name)); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* {@inheritDoc} |
310
|
|
|
*/ |
311
|
21 |
|
public function createNativeQuery($sql, ResultSetMapping $rsm) |
312
|
|
|
{ |
313
|
21 |
|
$query = new NativeQuery($this); |
314
|
|
|
|
315
|
21 |
|
$query->setSQL($sql); |
316
|
21 |
|
$query->setResultSetMapping($rsm); |
317
|
|
|
|
318
|
21 |
|
return $query; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* {@inheritDoc} |
323
|
|
|
*/ |
324
|
1 |
|
public function createNamedNativeQuery($name) |
325
|
|
|
{ |
326
|
1 |
|
list($sql, $rsm) = $this->config->getNamedNativeQuery($name); |
327
|
|
|
|
328
|
1 |
|
return $this->createNativeQuery($sql, $rsm); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* {@inheritDoc} |
333
|
|
|
*/ |
334
|
113 |
|
public function createQueryBuilder() |
335
|
|
|
{ |
336
|
113 |
|
return new QueryBuilder($this); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Flushes all changes to objects that have been queued up to now to the database. |
341
|
|
|
* This effectively synchronizes the in-memory state of managed objects with the |
342
|
|
|
* database. |
343
|
|
|
* |
344
|
|
|
* If an entity is explicitly passed to this method only this entity and |
345
|
|
|
* the cascade-persist semantics + scheduled inserts/removals are synchronized. |
346
|
|
|
* |
347
|
|
|
* @param null|object|array $entity |
348
|
|
|
* |
349
|
|
|
* @return void |
350
|
|
|
* |
351
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException If a version check on an entity that |
352
|
|
|
* makes use of optimistic locking fails. |
353
|
|
|
* @throws ORMException |
354
|
|
|
*/ |
355
|
1016 |
|
public function flush($entity = null) |
356
|
|
|
{ |
357
|
1016 |
|
$this->errorIfClosed(); |
358
|
|
|
|
359
|
1015 |
|
$this->unitOfWork->commit($entity); |
360
|
1006 |
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Finds an Entity by its identifier. |
364
|
|
|
* |
365
|
|
|
* @param string $entityName The class name of the entity to find. |
366
|
|
|
* @param mixed $id The identity of the entity to find. |
367
|
|
|
* @param integer|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants |
368
|
|
|
* or NULL if no specific lock mode should be used |
369
|
|
|
* during the search. |
370
|
|
|
* @param integer|null $lockVersion The version of the entity to find when using |
371
|
|
|
* optimistic locking. |
372
|
|
|
* |
373
|
|
|
* @return object|null The entity instance or NULL if the entity can not be found. |
374
|
|
|
* |
375
|
|
|
* @throws OptimisticLockException |
376
|
|
|
* @throws ORMInvalidArgumentException |
377
|
|
|
* @throws TransactionRequiredException |
378
|
|
|
* @throws ORMException |
379
|
|
|
*/ |
380
|
411 |
|
public function find($entityName, $id, $lockMode = null, $lockVersion = null) |
381
|
|
|
{ |
382
|
411 |
|
$class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\')); |
383
|
|
|
|
384
|
411 |
|
if ( ! is_array($id)) { |
385
|
367 |
|
if ($class->isIdentifierComposite) { |
|
|
|
|
386
|
|
|
throw ORMInvalidArgumentException::invalidCompositeIdentifier(); |
387
|
|
|
} |
388
|
|
|
|
389
|
367 |
|
$id = array($class->identifier[0] => $id); |
|
|
|
|
390
|
|
|
} |
391
|
|
|
|
392
|
411 |
|
foreach ($id as $i => $value) { |
393
|
411 |
|
if (is_object($value) && $this->metadataFactory->hasMetadataFor(ClassUtils::getClass($value))) { |
394
|
6 |
|
$id[$i] = $this->unitOfWork->getSingleIdentifierValue($value); |
395
|
|
|
|
396
|
6 |
|
if ($id[$i] === null) { |
397
|
411 |
|
throw ORMInvalidArgumentException::invalidIdentifierBindingEntity(); |
398
|
|
|
} |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
|
402
|
410 |
|
$sortedId = array(); |
403
|
|
|
|
404
|
410 |
|
foreach ($class->identifier as $identifier) { |
|
|
|
|
405
|
410 |
|
if ( ! isset($id[$identifier])) { |
406
|
1 |
|
throw ORMException::missingIdentifierField($class->name, $identifier); |
|
|
|
|
407
|
|
|
} |
408
|
|
|
|
409
|
409 |
|
$sortedId[$identifier] = $id[$identifier]; |
410
|
409 |
|
unset($id[$identifier]); |
411
|
|
|
} |
412
|
|
|
|
413
|
409 |
|
if ($id) { |
|
|
|
|
414
|
1 |
|
throw ORMException::unrecognizedIdentifierFields($class->name, array_keys($id)); |
|
|
|
|
415
|
|
|
} |
416
|
|
|
|
417
|
408 |
|
$unitOfWork = $this->getUnitOfWork(); |
418
|
|
|
|
419
|
|
|
// Check identity map first |
420
|
408 |
|
if (($entity = $unitOfWork->tryGetById($sortedId, $class->rootEntityName)) !== false) { |
|
|
|
|
421
|
41 |
|
if ( ! ($entity instanceof $class->name)) { |
|
|
|
|
422
|
1 |
|
return null; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
switch (true) { |
426
|
40 |
|
case LockMode::OPTIMISTIC === $lockMode: |
427
|
1 |
|
$this->lock($entity, $lockMode, $lockVersion); |
428
|
|
|
break; |
429
|
|
|
|
430
|
40 |
|
case LockMode::NONE === $lockMode: |
431
|
40 |
|
case LockMode::PESSIMISTIC_READ === $lockMode: |
432
|
40 |
|
case LockMode::PESSIMISTIC_WRITE === $lockMode: |
433
|
|
|
$persister = $unitOfWork->getEntityPersister($class->name); |
|
|
|
|
434
|
|
|
$persister->refresh($sortedId, $entity, $lockMode); |
435
|
|
|
break; |
436
|
|
|
} |
437
|
|
|
|
438
|
40 |
|
return $entity; // Hit! |
439
|
|
|
} |
440
|
|
|
|
441
|
393 |
|
$persister = $unitOfWork->getEntityPersister($class->name); |
|
|
|
|
442
|
|
|
|
443
|
|
|
switch (true) { |
444
|
393 |
|
case LockMode::OPTIMISTIC === $lockMode: |
445
|
1 |
|
if ( ! $class->isVersioned) { |
|
|
|
|
446
|
1 |
|
throw OptimisticLockException::notVersioned($class->name); |
|
|
|
|
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
$entity = $persister->load($sortedId); |
450
|
|
|
|
451
|
|
|
$unitOfWork->lock($entity, $lockMode, $lockVersion); |
|
|
|
|
452
|
|
|
|
453
|
|
|
return $entity; |
454
|
|
|
|
455
|
392 |
|
case LockMode::PESSIMISTIC_READ === $lockMode: |
456
|
391 |
|
case LockMode::PESSIMISTIC_WRITE === $lockMode: |
457
|
2 |
|
if ( ! $this->getConnection()->isTransactionActive()) { |
458
|
2 |
|
throw TransactionRequiredException::transactionRequired(); |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
return $persister->load($sortedId, null, null, array(), $lockMode); |
462
|
|
|
|
463
|
|
|
default: |
464
|
390 |
|
return $persister->loadById($sortedId); |
465
|
|
|
} |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* {@inheritDoc} |
470
|
|
|
*/ |
471
|
91 |
|
public function getReference($entityName, $id) |
472
|
|
|
{ |
473
|
91 |
|
$class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\')); |
474
|
|
|
|
475
|
91 |
|
if ( ! is_array($id)) { |
476
|
49 |
|
$id = array($class->identifier[0] => $id); |
|
|
|
|
477
|
|
|
} |
478
|
|
|
|
479
|
91 |
|
$sortedId = array(); |
480
|
|
|
|
481
|
91 |
|
foreach ($class->identifier as $identifier) { |
|
|
|
|
482
|
91 |
|
if ( ! isset($id[$identifier])) { |
483
|
|
|
throw ORMException::missingIdentifierField($class->name, $identifier); |
|
|
|
|
484
|
|
|
} |
485
|
|
|
|
486
|
91 |
|
$sortedId[$identifier] = $id[$identifier]; |
487
|
91 |
|
unset($id[$identifier]); |
488
|
|
|
} |
489
|
|
|
|
490
|
91 |
|
if ($id) { |
|
|
|
|
491
|
1 |
|
throw ORMException::unrecognizedIdentifierFields($class->name, array_keys($id)); |
|
|
|
|
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
// Check identity map first, if its already in there just return it. |
495
|
90 |
|
if (($entity = $this->unitOfWork->tryGetById($sortedId, $class->rootEntityName)) !== false) { |
|
|
|
|
496
|
27 |
|
return ($entity instanceof $class->name) ? $entity : null; |
|
|
|
|
497
|
|
|
} |
498
|
|
|
|
499
|
86 |
|
if ($class->subClasses) { |
|
|
|
|
500
|
2 |
|
return $this->find($entityName, $sortedId); |
501
|
|
|
} |
502
|
|
|
|
503
|
86 |
|
$entity = $this->proxyFactory->getProxy($class->name, $sortedId); |
|
|
|
|
504
|
|
|
|
505
|
86 |
|
$this->unitOfWork->registerManaged($entity, $sortedId, array()); |
506
|
|
|
|
507
|
86 |
|
return $entity; |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
/** |
511
|
|
|
* {@inheritDoc} |
512
|
|
|
*/ |
513
|
4 |
|
public function getPartialReference($entityName, $identifier) |
514
|
|
|
{ |
515
|
4 |
|
$class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\')); |
516
|
|
|
|
517
|
|
|
// Check identity map first, if its already in there just return it. |
518
|
4 |
|
if (($entity = $this->unitOfWork->tryGetById($identifier, $class->rootEntityName)) !== false) { |
|
|
|
|
519
|
1 |
|
return ($entity instanceof $class->name) ? $entity : null; |
|
|
|
|
520
|
|
|
} |
521
|
|
|
|
522
|
3 |
|
if ( ! is_array($identifier)) { |
523
|
3 |
|
$identifier = array($class->identifier[0] => $identifier); |
|
|
|
|
524
|
|
|
} |
525
|
|
|
|
526
|
3 |
|
$entity = $class->newInstance(); |
527
|
|
|
|
528
|
3 |
|
$class->setIdentifierValues($entity, $identifier); |
529
|
|
|
|
530
|
3 |
|
$this->unitOfWork->registerManaged($entity, $identifier, array()); |
531
|
3 |
|
$this->unitOfWork->markReadOnly($entity); |
532
|
|
|
|
533
|
3 |
|
return $entity; |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
/** |
537
|
|
|
* Clears the EntityManager. All entities that are currently managed |
538
|
|
|
* by this EntityManager become detached. |
539
|
|
|
* |
540
|
|
|
* @param string|null $entityName if given, only entities of this type will get detached |
541
|
|
|
* |
542
|
|
|
* @return void |
543
|
|
|
*/ |
544
|
1223 |
|
public function clear($entityName = null) |
545
|
|
|
{ |
546
|
1223 |
|
$this->unitOfWork->clear($entityName); |
547
|
1223 |
|
} |
548
|
|
|
|
549
|
|
|
/** |
550
|
|
|
* {@inheritDoc} |
551
|
|
|
*/ |
552
|
19 |
|
public function close() |
553
|
|
|
{ |
554
|
19 |
|
$this->clear(); |
555
|
|
|
|
556
|
19 |
|
$this->closed = true; |
557
|
19 |
|
} |
558
|
|
|
|
559
|
|
|
/** |
560
|
|
|
* Tells the EntityManager to make an instance managed and persistent. |
561
|
|
|
* |
562
|
|
|
* The entity will be entered into the database at or before transaction |
563
|
|
|
* commit or as a result of the flush operation. |
564
|
|
|
* |
565
|
|
|
* NOTE: The persist operation always considers entities that are not yet known to |
566
|
|
|
* this EntityManager as NEW. Do not pass detached entities to the persist operation. |
567
|
|
|
* |
568
|
|
|
* @param object $entity The instance to make managed and persistent. |
569
|
|
|
* |
570
|
|
|
* @return void |
571
|
|
|
* |
572
|
|
|
* @throws ORMInvalidArgumentException |
573
|
|
|
* @throws ORMException |
574
|
|
|
*/ |
575
|
1010 |
|
public function persist($entity) |
576
|
|
|
{ |
577
|
1010 |
|
if ( ! is_object($entity)) { |
578
|
1 |
|
throw ORMInvalidArgumentException::invalidObject('EntityManager#persist()', $entity); |
579
|
|
|
} |
580
|
|
|
|
581
|
1009 |
|
$this->errorIfClosed(); |
582
|
|
|
|
583
|
1008 |
|
$this->unitOfWork->persist($entity); |
584
|
1007 |
|
} |
585
|
|
|
|
586
|
|
|
/** |
587
|
|
|
* Removes an entity instance. |
588
|
|
|
* |
589
|
|
|
* A removed entity will be removed from the database at or before transaction commit |
590
|
|
|
* or as a result of the flush operation. |
591
|
|
|
* |
592
|
|
|
* @param object $entity The entity instance to remove. |
593
|
|
|
* |
594
|
|
|
* @return void |
595
|
|
|
* |
596
|
|
|
* @throws ORMInvalidArgumentException |
597
|
|
|
* @throws ORMException |
598
|
|
|
*/ |
599
|
51 |
|
public function remove($entity) |
600
|
|
|
{ |
601
|
51 |
|
if ( ! is_object($entity)) { |
602
|
1 |
|
throw ORMInvalidArgumentException::invalidObject('EntityManager#remove()', $entity); |
603
|
|
|
} |
604
|
|
|
|
605
|
50 |
|
$this->errorIfClosed(); |
606
|
|
|
|
607
|
49 |
|
$this->unitOfWork->remove($entity); |
608
|
49 |
|
} |
609
|
|
|
|
610
|
|
|
/** |
611
|
|
|
* Refreshes the persistent state of an entity from the database, |
612
|
|
|
* overriding any local changes that have not yet been persisted. |
613
|
|
|
* |
614
|
|
|
* @param object $entity The entity to refresh. |
615
|
|
|
* |
616
|
|
|
* @return void |
617
|
|
|
* |
618
|
|
|
* @throws ORMInvalidArgumentException |
619
|
|
|
* @throws ORMException |
620
|
|
|
*/ |
621
|
19 |
|
public function refresh($entity) |
622
|
|
|
{ |
623
|
19 |
|
if ( ! is_object($entity)) { |
624
|
1 |
|
throw ORMInvalidArgumentException::invalidObject('EntityManager#refresh()', $entity); |
625
|
|
|
} |
626
|
|
|
|
627
|
18 |
|
$this->errorIfClosed(); |
628
|
|
|
|
629
|
17 |
|
$this->unitOfWork->refresh($entity); |
630
|
17 |
|
} |
631
|
|
|
|
632
|
|
|
/** |
633
|
|
|
* Detaches an entity from the EntityManager, causing a managed entity to |
634
|
|
|
* become detached. Unflushed changes made to the entity if any |
635
|
|
|
* (including removal of the entity), will not be synchronized to the database. |
636
|
|
|
* Entities which previously referenced the detached entity will continue to |
637
|
|
|
* reference it. |
638
|
|
|
* |
639
|
|
|
* @param object $entity The entity to detach. |
640
|
|
|
* |
641
|
|
|
* @return void |
642
|
|
|
* |
643
|
|
|
* @throws ORMInvalidArgumentException |
644
|
|
|
*/ |
645
|
13 |
|
public function detach($entity) |
646
|
|
|
{ |
647
|
13 |
|
if ( ! is_object($entity)) { |
648
|
1 |
|
throw ORMInvalidArgumentException::invalidObject('EntityManager#detach()', $entity); |
649
|
|
|
} |
650
|
|
|
|
651
|
12 |
|
$this->unitOfWork->detach($entity); |
652
|
12 |
|
} |
653
|
|
|
|
654
|
|
|
/** |
655
|
|
|
* Merges the state of a detached entity into the persistence context |
656
|
|
|
* of this EntityManager and returns the managed copy of the entity. |
657
|
|
|
* The entity passed to merge will not become associated/managed with this EntityManager. |
658
|
|
|
* |
659
|
|
|
* @param object $entity The detached entity to merge into the persistence context. |
660
|
|
|
* |
661
|
|
|
* @return object The managed copy of the entity. |
662
|
|
|
* |
663
|
|
|
* @throws ORMInvalidArgumentException |
664
|
|
|
* @throws ORMException |
665
|
|
|
*/ |
666
|
42 |
|
public function merge($entity) |
667
|
|
|
{ |
668
|
42 |
|
if ( ! is_object($entity)) { |
669
|
1 |
|
throw ORMInvalidArgumentException::invalidObject('EntityManager#merge()', $entity); |
670
|
|
|
} |
671
|
|
|
|
672
|
41 |
|
$this->errorIfClosed(); |
673
|
|
|
|
674
|
40 |
|
return $this->unitOfWork->merge($entity); |
675
|
|
|
} |
676
|
|
|
|
677
|
|
|
/** |
678
|
|
|
* {@inheritDoc} |
679
|
|
|
* |
680
|
|
|
* @todo Implementation need. This is necessary since $e2 = clone $e1; throws an E_FATAL when access anything on $e: |
681
|
|
|
* Fatal error: Maximum function nesting level of '100' reached, aborting! |
682
|
|
|
*/ |
683
|
|
|
public function copy($entity, $deep = false) |
684
|
|
|
{ |
685
|
|
|
throw new \BadMethodCallException("Not implemented."); |
686
|
|
|
} |
687
|
|
|
|
688
|
|
|
/** |
689
|
|
|
* {@inheritDoc} |
690
|
|
|
*/ |
691
|
10 |
|
public function lock($entity, $lockMode, $lockVersion = null) |
692
|
|
|
{ |
693
|
10 |
|
$this->unitOfWork->lock($entity, $lockMode, $lockVersion); |
694
|
3 |
|
} |
695
|
|
|
|
696
|
|
|
/** |
697
|
|
|
* Gets the repository for an entity class. |
698
|
|
|
* |
699
|
|
|
* @param string $entityName The name of the entity. |
700
|
|
|
* |
701
|
|
|
* @return \Doctrine\ORM\EntityRepository The repository class. |
702
|
|
|
*/ |
703
|
147 |
|
public function getRepository($entityName) |
704
|
|
|
{ |
705
|
147 |
|
return $this->repositoryFactory->getRepository($this, $entityName); |
706
|
|
|
} |
707
|
|
|
|
708
|
|
|
/** |
709
|
|
|
* Determines whether an entity instance is managed in this EntityManager. |
710
|
|
|
* |
711
|
|
|
* @param object $entity |
712
|
|
|
* |
713
|
|
|
* @return boolean TRUE if this EntityManager currently manages the given entity, FALSE otherwise. |
714
|
|
|
*/ |
715
|
22 |
|
public function contains($entity) |
716
|
|
|
{ |
717
|
22 |
|
return $this->unitOfWork->isScheduledForInsert($entity) |
718
|
20 |
|
|| $this->unitOfWork->isInIdentityMap($entity) |
719
|
22 |
|
&& ! $this->unitOfWork->isScheduledForDelete($entity); |
720
|
|
|
} |
721
|
|
|
|
722
|
|
|
/** |
723
|
|
|
* {@inheritDoc} |
724
|
|
|
*/ |
725
|
2347 |
|
public function getEventManager() |
726
|
|
|
{ |
727
|
2347 |
|
return $this->eventManager; |
728
|
|
|
} |
729
|
|
|
|
730
|
|
|
/** |
731
|
|
|
* {@inheritDoc} |
732
|
|
|
*/ |
733
|
2347 |
|
public function getConfiguration() |
734
|
|
|
{ |
735
|
2347 |
|
return $this->config; |
736
|
|
|
} |
737
|
|
|
|
738
|
|
|
/** |
739
|
|
|
* Throws an exception if the EntityManager is closed or currently not active. |
740
|
|
|
* |
741
|
|
|
* @return void |
742
|
|
|
* |
743
|
|
|
* @throws ORMException If the EntityManager is closed. |
744
|
|
|
*/ |
745
|
1029 |
|
private function errorIfClosed() |
746
|
|
|
{ |
747
|
1029 |
|
if ($this->closed) { |
748
|
5 |
|
throw ORMException::entityManagerClosed(); |
749
|
|
|
} |
750
|
1024 |
|
} |
751
|
|
|
|
752
|
|
|
/** |
753
|
|
|
* {@inheritDoc} |
754
|
|
|
*/ |
755
|
1 |
|
public function isOpen() |
756
|
|
|
{ |
757
|
1 |
|
return (!$this->closed); |
758
|
|
|
} |
759
|
|
|
|
760
|
|
|
/** |
761
|
|
|
* {@inheritDoc} |
762
|
|
|
*/ |
763
|
2347 |
|
public function getUnitOfWork() |
764
|
|
|
{ |
765
|
2347 |
|
return $this->unitOfWork; |
766
|
|
|
} |
767
|
|
|
|
768
|
|
|
/** |
769
|
|
|
* {@inheritDoc} |
770
|
|
|
*/ |
771
|
|
|
public function getHydrator($hydrationMode) |
772
|
|
|
{ |
773
|
|
|
return $this->newHydrator($hydrationMode); |
774
|
|
|
} |
775
|
|
|
|
776
|
|
|
/** |
777
|
|
|
* {@inheritDoc} |
778
|
|
|
*/ |
779
|
885 |
|
public function newHydrator($hydrationMode) |
780
|
|
|
{ |
781
|
|
|
switch ($hydrationMode) { |
782
|
885 |
|
case Query::HYDRATE_OBJECT: |
783
|
617 |
|
return new Internal\Hydration\ObjectHydrator($this); |
784
|
|
|
|
785
|
537 |
|
case Query::HYDRATE_ARRAY: |
786
|
35 |
|
return new Internal\Hydration\ArrayHydrator($this); |
787
|
|
|
|
788
|
507 |
|
case Query::HYDRATE_SCALAR: |
789
|
85 |
|
return new Internal\Hydration\ScalarHydrator($this); |
790
|
|
|
|
791
|
424 |
|
case Query::HYDRATE_SINGLE_SCALAR: |
792
|
12 |
|
return new Internal\Hydration\SingleScalarHydrator($this); |
793
|
|
|
|
794
|
416 |
|
case Query::HYDRATE_SIMPLEOBJECT: |
795
|
415 |
|
return new Internal\Hydration\SimpleObjectHydrator($this); |
796
|
|
|
|
797
|
|
|
default: |
798
|
1 |
|
if (($class = $this->config->getCustomHydrationMode($hydrationMode)) !== null) { |
799
|
1 |
|
return new $class($this); |
800
|
|
|
} |
801
|
|
|
} |
802
|
|
|
|
803
|
|
|
throw ORMException::invalidHydrationMode($hydrationMode); |
804
|
|
|
} |
805
|
|
|
|
806
|
|
|
/** |
807
|
|
|
* {@inheritDoc} |
808
|
|
|
*/ |
809
|
165 |
|
public function getProxyFactory() |
810
|
|
|
{ |
811
|
165 |
|
return $this->proxyFactory; |
812
|
|
|
} |
813
|
|
|
|
814
|
|
|
/** |
815
|
|
|
* {@inheritDoc} |
816
|
|
|
*/ |
817
|
|
|
public function initializeObject($obj) |
818
|
|
|
{ |
819
|
|
|
$this->unitOfWork->initializeObject($obj); |
820
|
|
|
} |
821
|
|
|
|
822
|
|
|
/** |
823
|
|
|
* Factory method to create EntityManager instances. |
824
|
|
|
* |
825
|
|
|
* @param array|Connection $connection An array with the connection parameters or an existing Connection instance. |
826
|
|
|
* @param Configuration $config The Configuration instance to use. |
827
|
|
|
* @param EventManager $eventManager The EventManager instance to use. |
828
|
|
|
* |
829
|
|
|
* @return EntityManager The created EntityManager. |
830
|
|
|
* |
831
|
|
|
* @throws \InvalidArgumentException |
832
|
|
|
* @throws ORMException |
833
|
|
|
*/ |
834
|
1235 |
|
public static function create($connection, Configuration $config, EventManager $eventManager = null) |
835
|
|
|
{ |
836
|
1235 |
|
if ( ! $config->getMetadataDriverImpl()) { |
837
|
|
|
throw ORMException::missingMappingDriverImpl(); |
838
|
|
|
} |
839
|
|
|
|
840
|
1235 |
|
$connection = static::createConnection($connection, $config, $eventManager); |
841
|
|
|
|
842
|
1235 |
|
return new EntityManager($connection, $config, $connection->getEventManager()); |
843
|
|
|
} |
844
|
|
|
|
845
|
|
|
/** |
846
|
|
|
* Factory method to create Connection instances. |
847
|
|
|
* |
848
|
|
|
* @param array|Connection $connection An array with the connection parameters or an existing Connection instance. |
849
|
|
|
* @param Configuration $config The Configuration instance to use. |
850
|
|
|
* @param EventManager $eventManager The EventManager instance to use. |
851
|
|
|
* |
852
|
|
|
* @return Connection |
853
|
|
|
* |
854
|
|
|
* @throws \InvalidArgumentException |
855
|
|
|
* @throws ORMException |
856
|
|
|
*/ |
857
|
1235 |
|
protected static function createConnection($connection, Configuration $config, EventManager $eventManager = null) |
858
|
|
|
{ |
859
|
1235 |
|
if (is_array($connection)) { |
860
|
|
|
return DriverManager::getConnection($connection, $config, $eventManager ?: new EventManager()); |
861
|
|
|
} |
862
|
|
|
|
863
|
1235 |
|
if ( ! $connection instanceof Connection) { |
864
|
|
|
throw new \InvalidArgumentException("Invalid argument: " . $connection); |
865
|
|
|
} |
866
|
|
|
|
867
|
1235 |
|
if ($eventManager !== null && $connection->getEventManager() !== $eventManager) { |
868
|
|
|
throw ORMException::mismatchedEventManager(); |
869
|
|
|
} |
870
|
|
|
|
871
|
1235 |
|
return $connection; |
872
|
|
|
} |
873
|
|
|
|
874
|
|
|
/** |
875
|
|
|
* {@inheritDoc} |
876
|
|
|
*/ |
877
|
608 |
|
public function getFilters() |
878
|
|
|
{ |
879
|
608 |
|
if (null === $this->filterCollection) { |
880
|
608 |
|
$this->filterCollection = new FilterCollection($this); |
881
|
|
|
} |
882
|
|
|
|
883
|
608 |
|
return $this->filterCollection; |
884
|
|
|
} |
885
|
|
|
|
886
|
|
|
/** |
887
|
|
|
* {@inheritDoc} |
888
|
|
|
*/ |
889
|
37 |
|
public function isFiltersStateClean() |
890
|
|
|
{ |
891
|
37 |
|
return null === $this->filterCollection || $this->filterCollection->isClean(); |
892
|
|
|
} |
893
|
|
|
|
894
|
|
|
/** |
895
|
|
|
* {@inheritDoc} |
896
|
|
|
*/ |
897
|
736 |
|
public function hasFilters() |
898
|
|
|
{ |
899
|
736 |
|
return null !== $this->filterCollection; |
900
|
|
|
} |
901
|
|
|
} |
902
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: