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\Mapping\MappingException; |
23
|
|
|
use Exception; |
24
|
|
|
use Doctrine\Common\EventManager; |
25
|
|
|
use Doctrine\DBAL\Connection; |
26
|
|
|
use Doctrine\DBAL\DriverManager; |
27
|
|
|
use Doctrine\DBAL\LockMode; |
28
|
|
|
use Doctrine\ORM\Query\ResultSetMapping; |
29
|
|
|
use Doctrine\ORM\Proxy\ProxyFactory; |
30
|
|
|
use Doctrine\ORM\Query\FilterCollection; |
31
|
|
|
use Doctrine\Common\Util\ClassUtils; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The EntityManager is the central access point to ORM functionality. |
35
|
|
|
* |
36
|
|
|
* It is a facade to all different ORM subsystems such as UnitOfWork, |
37
|
|
|
* Query Language and Repository API. Instantiation is done through |
38
|
|
|
* the static create() method. The quickest way to obtain a fully |
39
|
|
|
* configured EntityManager is: |
40
|
|
|
* |
41
|
|
|
* use Doctrine\ORM\Tools\Setup; |
42
|
|
|
* use Doctrine\ORM\EntityManager; |
43
|
|
|
* |
44
|
|
|
* $paths = array('/path/to/entity/mapping/files'); |
45
|
|
|
* |
46
|
|
|
* $config = Setup::createAnnotationMetadataConfiguration($paths); |
47
|
|
|
* $dbParams = array('driver' => 'pdo_sqlite', 'memory' => true); |
48
|
|
|
* $entityManager = EntityManager::create($dbParams, $config); |
49
|
|
|
* |
50
|
|
|
* For more information see |
51
|
|
|
* {@link http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/configuration.html} |
52
|
|
|
* |
53
|
|
|
* You should never attempt to inherit from the EntityManager: Inheritance |
54
|
|
|
* is not a valid extension point for the EntityManager. Instead you |
55
|
|
|
* should take a look at the {@see \Doctrine\ORM\Decorator\EntityManagerDecorator} |
56
|
|
|
* and wrap your entity manager in a decorator. |
57
|
|
|
* |
58
|
|
|
* @since 2.0 |
59
|
|
|
* @author Benjamin Eberlei <[email protected]> |
60
|
|
|
* @author Guilherme Blanco <[email protected]> |
61
|
|
|
* @author Jonathan Wage <[email protected]> |
62
|
|
|
* @author Roman Borschel <[email protected]> |
63
|
|
|
*/ |
64
|
|
|
/* final */class EntityManager implements EntityManagerInterface |
65
|
|
|
{ |
66
|
|
|
/** |
67
|
|
|
* The used Configuration. |
68
|
|
|
* |
69
|
|
|
* @var \Doctrine\ORM\Configuration |
70
|
|
|
*/ |
71
|
|
|
private $config; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The database connection used by the EntityManager. |
75
|
|
|
* |
76
|
|
|
* @var \Doctrine\DBAL\Connection |
77
|
|
|
*/ |
78
|
|
|
private $conn; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* The metadata factory, used to retrieve the ORM metadata of entity classes. |
82
|
|
|
* |
83
|
|
|
* @var \Doctrine\ORM\Mapping\ClassMetadataFactory |
84
|
|
|
*/ |
85
|
|
|
private $metadataFactory; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* The UnitOfWork used to coordinate object-level transactions. |
89
|
|
|
* |
90
|
|
|
* @var \Doctrine\ORM\UnitOfWork |
91
|
|
|
*/ |
92
|
|
|
private $unitOfWork; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* The event manager that is the central point of the event system. |
96
|
|
|
* |
97
|
|
|
* @var \Doctrine\Common\EventManager |
98
|
|
|
*/ |
99
|
|
|
private $eventManager; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* The proxy factory used to create dynamic proxies. |
103
|
|
|
* |
104
|
|
|
* @var \Doctrine\ORM\Proxy\ProxyFactory |
105
|
|
|
*/ |
106
|
|
|
private $proxyFactory; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* The repository factory used to create dynamic repositories. |
110
|
|
|
* |
111
|
|
|
* @var \Doctrine\ORM\Repository\RepositoryFactory |
112
|
|
|
*/ |
113
|
|
|
private $repositoryFactory; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* The expression builder instance used to generate query expressions. |
117
|
|
|
* |
118
|
|
|
* @var \Doctrine\ORM\Query\Expr |
119
|
|
|
*/ |
120
|
|
|
private $expressionBuilder; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Whether the EntityManager is closed or not. |
124
|
|
|
* |
125
|
|
|
* @var bool |
126
|
|
|
*/ |
127
|
|
|
private $closed = false; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Collection of query filters. |
131
|
|
|
* |
132
|
|
|
* @var \Doctrine\ORM\Query\FilterCollection |
133
|
|
|
*/ |
134
|
|
|
private $filterCollection; |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @var \Doctrine\ORM\Cache The second level cache regions API. |
138
|
|
|
*/ |
139
|
|
|
private $cache; |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Creates a new EntityManager that operates on the given database connection |
143
|
|
|
* and uses the given Configuration and EventManager implementations. |
144
|
|
|
* |
145
|
|
|
* @param \Doctrine\DBAL\Connection $conn |
146
|
|
|
* @param \Doctrine\ORM\Configuration $config |
147
|
|
|
* @param \Doctrine\Common\EventManager $eventManager |
148
|
|
|
*/ |
149
|
2360 |
|
protected function __construct(Connection $conn, Configuration $config, EventManager $eventManager) |
150
|
|
|
{ |
151
|
2360 |
|
$this->conn = $conn; |
152
|
2360 |
|
$this->config = $config; |
153
|
2360 |
|
$this->eventManager = $eventManager; |
154
|
|
|
|
155
|
2360 |
|
$metadataFactoryClassName = $config->getClassMetadataFactoryName(); |
156
|
|
|
|
157
|
2360 |
|
$this->metadataFactory = new $metadataFactoryClassName; |
158
|
2360 |
|
$this->metadataFactory->setEntityManager($this); |
159
|
2360 |
|
$this->metadataFactory->setCacheDriver($this->config->getMetadataCacheImpl()); |
160
|
|
|
|
161
|
2360 |
|
$this->repositoryFactory = $config->getRepositoryFactory(); |
162
|
2360 |
|
$this->unitOfWork = new UnitOfWork($this); |
163
|
2360 |
|
$this->proxyFactory = new ProxyFactory( |
164
|
|
|
$this, |
165
|
2360 |
|
$config->getProxyDir(), |
166
|
2360 |
|
$config->getProxyNamespace(), |
167
|
2360 |
|
$config->getAutoGenerateProxyClasses() |
168
|
|
|
); |
169
|
|
|
|
170
|
2360 |
|
if ($config->isSecondLevelCacheEnabled()) { |
171
|
280 |
|
$cacheConfig = $config->getSecondLevelCacheConfiguration(); |
172
|
280 |
|
$cacheFactory = $cacheConfig->getCacheFactory(); |
173
|
280 |
|
$this->cache = $cacheFactory->createCache($this); |
174
|
|
|
} |
175
|
2360 |
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritDoc} |
179
|
|
|
*/ |
180
|
1848 |
|
public function getConnection() |
181
|
|
|
{ |
182
|
1848 |
|
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
|
2360 |
|
public function getMetadataFactory() |
191
|
|
|
{ |
192
|
2360 |
|
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
|
214 |
|
public function getCache() |
219
|
|
|
{ |
220
|
214 |
|
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
|
1914 |
|
public function getClassMetadata($className) |
282
|
|
|
{ |
283
|
1914 |
|
return $this->metadataFactory->getMetadataFor($className); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* {@inheritDoc} |
288
|
|
|
*/ |
289
|
928 |
|
public function createQuery($dql = '') |
290
|
|
|
{ |
291
|
928 |
|
$query = new Query($this); |
292
|
|
|
|
293
|
928 |
|
if ( ! empty($dql)) { |
294
|
923 |
|
$query->setDQL($dql); |
295
|
|
|
} |
296
|
|
|
|
297
|
928 |
|
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
|
114 |
|
public function createQueryBuilder() |
335
|
|
|
{ |
336
|
114 |
|
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
|
1021 |
|
public function flush($entity = null) |
356
|
|
|
{ |
357
|
1021 |
|
$this->errorIfClosed(); |
358
|
|
|
|
359
|
1020 |
|
$this->unitOfWork->commit($entity); |
360
|
1011 |
|
} |
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
|
414 |
|
public function find($entityName, $id, $lockMode = null, $lockVersion = null) |
381
|
|
|
{ |
382
|
414 |
|
$class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\')); |
383
|
|
|
|
384
|
414 |
|
if ( ! is_array($id)) { |
385
|
370 |
|
if ($class->isIdentifierComposite) { |
|
|
|
|
386
|
|
|
throw ORMInvalidArgumentException::invalidCompositeIdentifier(); |
387
|
|
|
} |
388
|
|
|
|
389
|
370 |
|
$id = array($class->identifier[0] => $id); |
|
|
|
|
390
|
|
|
} |
391
|
|
|
|
392
|
414 |
|
foreach ($id as $i => $value) { |
393
|
414 |
|
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
|
414 |
|
throw ORMInvalidArgumentException::invalidIdentifierBindingEntity(); |
398
|
|
|
} |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
|
402
|
413 |
|
$sortedId = array(); |
403
|
|
|
|
404
|
413 |
|
foreach ($class->identifier as $identifier) { |
|
|
|
|
405
|
413 |
|
if ( ! isset($id[$identifier])) { |
406
|
1 |
|
throw ORMException::missingIdentifierField($class->name, $identifier); |
|
|
|
|
407
|
|
|
} |
408
|
|
|
|
409
|
412 |
|
$sortedId[$identifier] = $id[$identifier]; |
410
|
412 |
|
unset($id[$identifier]); |
411
|
|
|
} |
412
|
|
|
|
413
|
412 |
|
if ($id) { |
|
|
|
|
414
|
1 |
|
throw ORMException::unrecognizedIdentifierFields($class->name, array_keys($id)); |
|
|
|
|
415
|
|
|
} |
416
|
|
|
|
417
|
411 |
|
$unitOfWork = $this->getUnitOfWork(); |
418
|
|
|
|
419
|
|
|
// Check identity map first |
420
|
411 |
|
if (($entity = $unitOfWork->tryGetById($sortedId, $class->rootEntityName)) !== false) { |
|
|
|
|
421
|
42 |
|
if ( ! ($entity instanceof $class->name)) { |
|
|
|
|
422
|
1 |
|
return null; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
switch (true) { |
426
|
41 |
|
case LockMode::OPTIMISTIC === $lockMode: |
427
|
1 |
|
$this->lock($entity, $lockMode, $lockVersion); |
428
|
|
|
break; |
429
|
|
|
|
430
|
41 |
|
case LockMode::NONE === $lockMode: |
431
|
41 |
|
case LockMode::PESSIMISTIC_READ === $lockMode: |
432
|
41 |
|
case LockMode::PESSIMISTIC_WRITE === $lockMode: |
433
|
|
|
$persister = $unitOfWork->getEntityPersister($class->name); |
|
|
|
|
434
|
|
|
$persister->refresh($sortedId, $entity, $lockMode); |
435
|
|
|
break; |
436
|
|
|
} |
437
|
|
|
|
438
|
41 |
|
return $entity; // Hit! |
439
|
|
|
} |
440
|
|
|
|
441
|
395 |
|
$persister = $unitOfWork->getEntityPersister($class->name); |
|
|
|
|
442
|
|
|
|
443
|
|
|
switch (true) { |
444
|
395 |
|
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
|
394 |
|
case LockMode::PESSIMISTIC_READ === $lockMode: |
456
|
393 |
|
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
|
392 |
|
return $persister->loadById($sortedId); |
465
|
|
|
} |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* {@inheritDoc} |
470
|
|
|
*/ |
471
|
92 |
|
public function getReference($entityName, $id) |
472
|
|
|
{ |
473
|
92 |
|
$class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\')); |
474
|
|
|
|
475
|
92 |
|
if ( ! is_array($id)) { |
476
|
49 |
|
$id = array($class->identifier[0] => $id); |
|
|
|
|
477
|
|
|
} |
478
|
|
|
|
479
|
92 |
|
$sortedId = array(); |
480
|
|
|
|
481
|
92 |
|
foreach ($class->identifier as $identifier) { |
|
|
|
|
482
|
92 |
|
if ( ! isset($id[$identifier])) { |
483
|
|
|
throw ORMException::missingIdentifierField($class->name, $identifier); |
|
|
|
|
484
|
|
|
} |
485
|
|
|
|
486
|
92 |
|
$sortedId[$identifier] = $id[$identifier]; |
487
|
92 |
|
unset($id[$identifier]); |
488
|
|
|
} |
489
|
|
|
|
490
|
92 |
|
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
|
91 |
|
if (($entity = $this->unitOfWork->tryGetById($sortedId, $class->rootEntityName)) !== false) { |
|
|
|
|
496
|
27 |
|
return ($entity instanceof $class->name) ? $entity : null; |
|
|
|
|
497
|
|
|
} |
498
|
|
|
|
499
|
87 |
|
if ($class->subClasses) { |
|
|
|
|
500
|
2 |
|
return $this->find($entityName, $sortedId); |
501
|
|
|
} |
502
|
|
|
|
503
|
87 |
|
$entity = $this->proxyFactory->getProxy($class->name, $sortedId); |
|
|
|
|
504
|
|
|
|
505
|
87 |
|
$this->unitOfWork->registerManaged($entity, $sortedId, array()); |
506
|
|
|
|
507
|
87 |
|
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
|
|
|
* @throws ORMInvalidArgumentException if a non-null non-string value is given |
545
|
|
|
* @throws \Doctrine\Common\Persistence\Mapping\MappingException if a $entityName is given, but that entity is not |
546
|
|
|
* found in the mappings |
547
|
|
|
*/ |
548
|
1232 |
|
public function clear($entityName = null) |
549
|
|
|
{ |
550
|
1232 |
|
if (null !== $entityName && ! is_string($entityName)) { |
551
|
1 |
|
throw ORMInvalidArgumentException::invalidEntityName($entityName); |
552
|
|
|
} |
553
|
|
|
|
554
|
1231 |
|
$this->unitOfWork->clear( |
555
|
1231 |
|
null === $entityName |
556
|
1229 |
|
? null |
557
|
1231 |
|
: $this->metadataFactory->getMetadataFor($entityName)->getName() |
558
|
|
|
); |
559
|
1230 |
|
} |
560
|
|
|
|
561
|
|
|
/** |
562
|
|
|
* {@inheritDoc} |
563
|
|
|
*/ |
564
|
19 |
|
public function close() |
565
|
|
|
{ |
566
|
19 |
|
$this->clear(); |
567
|
|
|
|
568
|
19 |
|
$this->closed = true; |
569
|
19 |
|
} |
570
|
|
|
|
571
|
|
|
/** |
572
|
|
|
* Tells the EntityManager to make an instance managed and persistent. |
573
|
|
|
* |
574
|
|
|
* The entity will be entered into the database at or before transaction |
575
|
|
|
* commit or as a result of the flush operation. |
576
|
|
|
* |
577
|
|
|
* NOTE: The persist operation always considers entities that are not yet known to |
578
|
|
|
* this EntityManager as NEW. Do not pass detached entities to the persist operation. |
579
|
|
|
* |
580
|
|
|
* @param object $entity The instance to make managed and persistent. |
581
|
|
|
* |
582
|
|
|
* @return void |
583
|
|
|
* |
584
|
|
|
* @throws ORMInvalidArgumentException |
585
|
|
|
* @throws ORMException |
586
|
|
|
*/ |
587
|
1017 |
|
public function persist($entity) |
588
|
|
|
{ |
589
|
1017 |
|
if ( ! is_object($entity)) { |
590
|
1 |
|
throw ORMInvalidArgumentException::invalidObject('EntityManager#persist()', $entity); |
591
|
|
|
} |
592
|
|
|
|
593
|
1016 |
|
$this->errorIfClosed(); |
594
|
|
|
|
595
|
1015 |
|
$this->unitOfWork->persist($entity); |
596
|
1014 |
|
} |
597
|
|
|
|
598
|
|
|
/** |
599
|
|
|
* Removes an entity instance. |
600
|
|
|
* |
601
|
|
|
* A removed entity will be removed from the database at or before transaction commit |
602
|
|
|
* or as a result of the flush operation. |
603
|
|
|
* |
604
|
|
|
* @param object $entity The entity instance to remove. |
605
|
|
|
* |
606
|
|
|
* @return void |
607
|
|
|
* |
608
|
|
|
* @throws ORMInvalidArgumentException |
609
|
|
|
* @throws ORMException |
610
|
|
|
*/ |
611
|
51 |
|
public function remove($entity) |
612
|
|
|
{ |
613
|
51 |
|
if ( ! is_object($entity)) { |
614
|
1 |
|
throw ORMInvalidArgumentException::invalidObject('EntityManager#remove()', $entity); |
615
|
|
|
} |
616
|
|
|
|
617
|
50 |
|
$this->errorIfClosed(); |
618
|
|
|
|
619
|
49 |
|
$this->unitOfWork->remove($entity); |
620
|
49 |
|
} |
621
|
|
|
|
622
|
|
|
/** |
623
|
|
|
* Refreshes the persistent state of an entity from the database, |
624
|
|
|
* overriding any local changes that have not yet been persisted. |
625
|
|
|
* |
626
|
|
|
* @param object $entity The entity to refresh. |
627
|
|
|
* |
628
|
|
|
* @return void |
629
|
|
|
* |
630
|
|
|
* @throws ORMInvalidArgumentException |
631
|
|
|
* @throws ORMException |
632
|
|
|
*/ |
633
|
19 |
|
public function refresh($entity) |
634
|
|
|
{ |
635
|
19 |
|
if ( ! is_object($entity)) { |
636
|
1 |
|
throw ORMInvalidArgumentException::invalidObject('EntityManager#refresh()', $entity); |
637
|
|
|
} |
638
|
|
|
|
639
|
18 |
|
$this->errorIfClosed(); |
640
|
|
|
|
641
|
17 |
|
$this->unitOfWork->refresh($entity); |
642
|
17 |
|
} |
643
|
|
|
|
644
|
|
|
/** |
645
|
|
|
* Detaches an entity from the EntityManager, causing a managed entity to |
646
|
|
|
* become detached. Unflushed changes made to the entity if any |
647
|
|
|
* (including removal of the entity), will not be synchronized to the database. |
648
|
|
|
* Entities which previously referenced the detached entity will continue to |
649
|
|
|
* reference it. |
650
|
|
|
* |
651
|
|
|
* @param object $entity The entity to detach. |
652
|
|
|
* |
653
|
|
|
* @return void |
654
|
|
|
* |
655
|
|
|
* @throws ORMInvalidArgumentException |
656
|
|
|
*/ |
657
|
13 |
|
public function detach($entity) |
658
|
|
|
{ |
659
|
13 |
|
if ( ! is_object($entity)) { |
660
|
1 |
|
throw ORMInvalidArgumentException::invalidObject('EntityManager#detach()', $entity); |
661
|
|
|
} |
662
|
|
|
|
663
|
12 |
|
$this->unitOfWork->detach($entity); |
664
|
12 |
|
} |
665
|
|
|
|
666
|
|
|
/** |
667
|
|
|
* Merges the state of a detached entity into the persistence context |
668
|
|
|
* of this EntityManager and returns the managed copy of the entity. |
669
|
|
|
* The entity passed to merge will not become associated/managed with this EntityManager. |
670
|
|
|
* |
671
|
|
|
* @param object $entity The detached entity to merge into the persistence context. |
672
|
|
|
* |
673
|
|
|
* @return object The managed copy of the entity. |
674
|
|
|
* |
675
|
|
|
* @throws ORMInvalidArgumentException |
676
|
|
|
* @throws ORMException |
677
|
|
|
*/ |
678
|
42 |
|
public function merge($entity) |
679
|
|
|
{ |
680
|
42 |
|
if ( ! is_object($entity)) { |
681
|
1 |
|
throw ORMInvalidArgumentException::invalidObject('EntityManager#merge()', $entity); |
682
|
|
|
} |
683
|
|
|
|
684
|
41 |
|
$this->errorIfClosed(); |
685
|
|
|
|
686
|
40 |
|
return $this->unitOfWork->merge($entity); |
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
/** |
690
|
|
|
* {@inheritDoc} |
691
|
|
|
* |
692
|
|
|
* @todo Implementation need. This is necessary since $e2 = clone $e1; throws an E_FATAL when access anything on $e: |
693
|
|
|
* Fatal error: Maximum function nesting level of '100' reached, aborting! |
694
|
|
|
*/ |
695
|
|
|
public function copy($entity, $deep = false) |
696
|
|
|
{ |
697
|
|
|
throw new \BadMethodCallException("Not implemented."); |
698
|
|
|
} |
699
|
|
|
|
700
|
|
|
/** |
701
|
|
|
* {@inheritDoc} |
702
|
|
|
*/ |
703
|
10 |
|
public function lock($entity, $lockMode, $lockVersion = null) |
704
|
|
|
{ |
705
|
10 |
|
$this->unitOfWork->lock($entity, $lockMode, $lockVersion); |
706
|
3 |
|
} |
707
|
|
|
|
708
|
|
|
/** |
709
|
|
|
* Gets the repository for an entity class. |
710
|
|
|
* |
711
|
|
|
* @param string $entityName The name of the entity. |
712
|
|
|
* |
713
|
|
|
* @return \Doctrine\ORM\EntityRepository The repository class. |
714
|
|
|
*/ |
715
|
147 |
|
public function getRepository($entityName) |
716
|
|
|
{ |
717
|
147 |
|
return $this->repositoryFactory->getRepository($this, $entityName); |
718
|
|
|
} |
719
|
|
|
|
720
|
|
|
/** |
721
|
|
|
* Determines whether an entity instance is managed in this EntityManager. |
722
|
|
|
* |
723
|
|
|
* @param object $entity |
724
|
|
|
* |
725
|
|
|
* @return boolean TRUE if this EntityManager currently manages the given entity, FALSE otherwise. |
726
|
|
|
*/ |
727
|
24 |
|
public function contains($entity) |
728
|
|
|
{ |
729
|
24 |
|
return $this->unitOfWork->isScheduledForInsert($entity) |
730
|
22 |
|
|| $this->unitOfWork->isInIdentityMap($entity) |
731
|
24 |
|
&& ! $this->unitOfWork->isScheduledForDelete($entity); |
732
|
|
|
} |
733
|
|
|
|
734
|
|
|
/** |
735
|
|
|
* {@inheritDoc} |
736
|
|
|
*/ |
737
|
2360 |
|
public function getEventManager() |
738
|
|
|
{ |
739
|
2360 |
|
return $this->eventManager; |
740
|
|
|
} |
741
|
|
|
|
742
|
|
|
/** |
743
|
|
|
* {@inheritDoc} |
744
|
|
|
*/ |
745
|
2360 |
|
public function getConfiguration() |
746
|
|
|
{ |
747
|
2360 |
|
return $this->config; |
748
|
|
|
} |
749
|
|
|
|
750
|
|
|
/** |
751
|
|
|
* Throws an exception if the EntityManager is closed or currently not active. |
752
|
|
|
* |
753
|
|
|
* @return void |
754
|
|
|
* |
755
|
|
|
* @throws ORMException If the EntityManager is closed. |
756
|
|
|
*/ |
757
|
1036 |
|
private function errorIfClosed() |
758
|
|
|
{ |
759
|
1036 |
|
if ($this->closed) { |
760
|
5 |
|
throw ORMException::entityManagerClosed(); |
761
|
|
|
} |
762
|
1031 |
|
} |
763
|
|
|
|
764
|
|
|
/** |
765
|
|
|
* {@inheritDoc} |
766
|
|
|
*/ |
767
|
1 |
|
public function isOpen() |
768
|
|
|
{ |
769
|
1 |
|
return (!$this->closed); |
770
|
|
|
} |
771
|
|
|
|
772
|
|
|
/** |
773
|
|
|
* {@inheritDoc} |
774
|
|
|
*/ |
775
|
2360 |
|
public function getUnitOfWork() |
776
|
|
|
{ |
777
|
2360 |
|
return $this->unitOfWork; |
778
|
|
|
} |
779
|
|
|
|
780
|
|
|
/** |
781
|
|
|
* {@inheritDoc} |
782
|
|
|
*/ |
783
|
|
|
public function getHydrator($hydrationMode) |
784
|
|
|
{ |
785
|
|
|
return $this->newHydrator($hydrationMode); |
786
|
|
|
} |
787
|
|
|
|
788
|
|
|
/** |
789
|
|
|
* {@inheritDoc} |
790
|
|
|
*/ |
791
|
891 |
|
public function newHydrator($hydrationMode) |
792
|
|
|
{ |
793
|
|
|
switch ($hydrationMode) { |
794
|
891 |
|
case Query::HYDRATE_OBJECT: |
795
|
622 |
|
return new Internal\Hydration\ObjectHydrator($this); |
796
|
|
|
|
797
|
540 |
|
case Query::HYDRATE_ARRAY: |
798
|
36 |
|
return new Internal\Hydration\ArrayHydrator($this); |
799
|
|
|
|
800
|
509 |
|
case Query::HYDRATE_SCALAR: |
801
|
85 |
|
return new Internal\Hydration\ScalarHydrator($this); |
802
|
|
|
|
803
|
426 |
|
case Query::HYDRATE_SINGLE_SCALAR: |
804
|
12 |
|
return new Internal\Hydration\SingleScalarHydrator($this); |
805
|
|
|
|
806
|
418 |
|
case Query::HYDRATE_SIMPLEOBJECT: |
807
|
417 |
|
return new Internal\Hydration\SimpleObjectHydrator($this); |
808
|
|
|
|
809
|
|
|
default: |
810
|
1 |
|
if (($class = $this->config->getCustomHydrationMode($hydrationMode)) !== null) { |
811
|
1 |
|
return new $class($this); |
812
|
|
|
} |
813
|
|
|
} |
814
|
|
|
|
815
|
|
|
throw ORMException::invalidHydrationMode($hydrationMode); |
816
|
|
|
} |
817
|
|
|
|
818
|
|
|
/** |
819
|
|
|
* {@inheritDoc} |
820
|
|
|
*/ |
821
|
167 |
|
public function getProxyFactory() |
822
|
|
|
{ |
823
|
167 |
|
return $this->proxyFactory; |
824
|
|
|
} |
825
|
|
|
|
826
|
|
|
/** |
827
|
|
|
* {@inheritDoc} |
828
|
|
|
*/ |
829
|
|
|
public function initializeObject($obj) |
830
|
|
|
{ |
831
|
|
|
$this->unitOfWork->initializeObject($obj); |
832
|
|
|
} |
833
|
|
|
|
834
|
|
|
/** |
835
|
|
|
* Factory method to create EntityManager instances. |
836
|
|
|
* |
837
|
|
|
* @param array|Connection $connection An array with the connection parameters or an existing Connection instance. |
838
|
|
|
* @param Configuration $config The Configuration instance to use. |
839
|
|
|
* @param EventManager $eventManager The EventManager instance to use. |
840
|
|
|
* |
841
|
|
|
* @return EntityManager The created EntityManager. |
842
|
|
|
* |
843
|
|
|
* @throws \InvalidArgumentException |
844
|
|
|
* @throws ORMException |
845
|
|
|
*/ |
846
|
1241 |
|
public static function create($connection, Configuration $config, EventManager $eventManager = null) |
847
|
|
|
{ |
848
|
1241 |
|
if ( ! $config->getMetadataDriverImpl()) { |
849
|
|
|
throw ORMException::missingMappingDriverImpl(); |
850
|
|
|
} |
851
|
|
|
|
852
|
1241 |
|
$connection = static::createConnection($connection, $config, $eventManager); |
853
|
|
|
|
854
|
1240 |
|
return new EntityManager($connection, $config, $connection->getEventManager()); |
855
|
|
|
} |
856
|
|
|
|
857
|
|
|
/** |
858
|
|
|
* Factory method to create Connection instances. |
859
|
|
|
* |
860
|
|
|
* @param array|Connection $connection An array with the connection parameters or an existing Connection instance. |
861
|
|
|
* @param Configuration $config The Configuration instance to use. |
862
|
|
|
* @param EventManager $eventManager The EventManager instance to use. |
863
|
|
|
* |
864
|
|
|
* @return Connection |
865
|
|
|
* |
866
|
|
|
* @throws \InvalidArgumentException |
867
|
|
|
* @throws ORMException |
868
|
|
|
*/ |
869
|
1241 |
|
protected static function createConnection($connection, Configuration $config, EventManager $eventManager = null) |
870
|
|
|
{ |
871
|
1241 |
|
if (is_array($connection)) { |
872
|
|
|
return DriverManager::getConnection($connection, $config, $eventManager ?: new EventManager()); |
873
|
|
|
} |
874
|
|
|
|
875
|
1241 |
|
if ( ! $connection instanceof Connection) { |
876
|
1 |
|
throw new \InvalidArgumentException( |
877
|
|
|
sprintf( |
878
|
1 |
|
'Invalid $connection argument of type %s given%s.', |
879
|
1 |
|
is_object($connection) ? get_class($connection) : gettype($connection), |
880
|
1 |
|
is_object($connection) ? '' : ': "' . $connection . '"' |
881
|
|
|
) |
882
|
|
|
); |
883
|
|
|
} |
884
|
|
|
|
885
|
1240 |
|
if ($eventManager !== null && $connection->getEventManager() !== $eventManager) { |
886
|
|
|
throw ORMException::mismatchedEventManager(); |
887
|
|
|
} |
888
|
|
|
|
889
|
1240 |
|
return $connection; |
890
|
|
|
} |
891
|
|
|
|
892
|
|
|
/** |
893
|
|
|
* {@inheritDoc} |
894
|
|
|
*/ |
895
|
612 |
|
public function getFilters() |
896
|
|
|
{ |
897
|
612 |
|
if (null === $this->filterCollection) { |
898
|
612 |
|
$this->filterCollection = new FilterCollection($this); |
899
|
|
|
} |
900
|
|
|
|
901
|
612 |
|
return $this->filterCollection; |
902
|
|
|
} |
903
|
|
|
|
904
|
|
|
/** |
905
|
|
|
* {@inheritDoc} |
906
|
|
|
*/ |
907
|
39 |
|
public function isFiltersStateClean() |
908
|
|
|
{ |
909
|
39 |
|
return null === $this->filterCollection || $this->filterCollection->isClean(); |
910
|
|
|
} |
911
|
|
|
|
912
|
|
|
/** |
913
|
|
|
* {@inheritDoc} |
914
|
|
|
*/ |
915
|
739 |
|
public function hasFilters() |
916
|
|
|
{ |
917
|
739 |
|
return null !== $this->filterCollection; |
918
|
|
|
} |
919
|
|
|
} |
920
|
|
|
|
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: