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\Common\EventManager; |
||
23 | use Doctrine\DBAL\Connection; |
||
24 | use Doctrine\DBAL\DriverManager; |
||
25 | use Doctrine\DBAL\LockMode; |
||
26 | use Doctrine\ORM\Query\ResultSetMapping; |
||
27 | use Doctrine\ORM\Proxy\ProxyFactory; |
||
28 | use Doctrine\ORM\Query\FilterCollection; |
||
29 | use Doctrine\Common\Util\ClassUtils; |
||
30 | use Throwable; |
||
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 | 2463 | protected function __construct(Connection $conn, Configuration $config, EventManager $eventManager) |
|
149 | { |
||
150 | 2463 | $this->conn = $conn; |
|
151 | 2463 | $this->config = $config; |
|
152 | 2463 | $this->eventManager = $eventManager; |
|
153 | |||
154 | 2463 | $metadataFactoryClassName = $config->getClassMetadataFactoryName(); |
|
155 | |||
156 | 2463 | $this->metadataFactory = new $metadataFactoryClassName; |
|
157 | 2463 | $this->metadataFactory->setEntityManager($this); |
|
158 | 2463 | $this->metadataFactory->setCacheDriver($this->config->getMetadataCacheImpl()); |
|
159 | |||
160 | 2463 | $this->repositoryFactory = $config->getRepositoryFactory(); |
|
161 | 2463 | $this->unitOfWork = new UnitOfWork($this); |
|
162 | 2463 | $this->proxyFactory = new ProxyFactory( |
|
163 | 2463 | $this, |
|
164 | 2463 | $config->getProxyDir(), |
|
165 | 2463 | $config->getProxyNamespace(), |
|
166 | 2463 | $config->getAutoGenerateProxyClasses() |
|
167 | ); |
||
168 | |||
169 | 2463 | if ($config->isSecondLevelCacheEnabled()) { |
|
170 | 286 | $cacheConfig = $config->getSecondLevelCacheConfiguration(); |
|
171 | 286 | $cacheFactory = $cacheConfig->getCacheFactory(); |
|
172 | 286 | $this->cache = $cacheFactory->createCache($this); |
|
173 | } |
||
174 | 2463 | } |
|
175 | |||
176 | /** |
||
177 | * {@inheritDoc} |
||
178 | */ |
||
179 | 1935 | public function getConnection() |
|
180 | { |
||
181 | 1935 | return $this->conn; |
|
182 | } |
||
183 | |||
184 | /** |
||
185 | * Gets the metadata factory used to gather the metadata of classes. |
||
186 | * |
||
187 | * @return \Doctrine\ORM\Mapping\ClassMetadataFactory |
||
188 | */ |
||
189 | 2463 | public function getMetadataFactory() |
|
190 | { |
||
191 | 2463 | return $this->metadataFactory; |
|
192 | } |
||
193 | |||
194 | /** |
||
195 | * {@inheritDoc} |
||
196 | */ |
||
197 | 17 | public function getExpressionBuilder() |
|
198 | { |
||
199 | 17 | if ($this->expressionBuilder === null) { |
|
200 | 17 | $this->expressionBuilder = new Query\Expr; |
|
201 | } |
||
202 | |||
203 | 17 | return $this->expressionBuilder; |
|
204 | } |
||
205 | |||
206 | /** |
||
207 | * {@inheritDoc} |
||
208 | */ |
||
209 | 1 | public function beginTransaction() |
|
210 | { |
||
211 | 1 | $this->conn->beginTransaction(); |
|
212 | 1 | } |
|
213 | |||
214 | /** |
||
215 | * {@inheritDoc} |
||
216 | */ |
||
217 | 218 | public function getCache() |
|
218 | { |
||
219 | 218 | return $this->cache; |
|
220 | } |
||
221 | |||
222 | /** |
||
223 | * {@inheritDoc} |
||
224 | */ |
||
225 | 5 | public function transactional($func) |
|
226 | { |
||
227 | 5 | if (!is_callable($func)) { |
|
228 | 1 | throw new \InvalidArgumentException('Expected argument of type "callable", got "' . gettype($func) . '"'); |
|
229 | } |
||
230 | |||
231 | 4 | $this->conn->beginTransaction(); |
|
232 | |||
233 | try { |
||
234 | 4 | $return = call_user_func($func, $this); |
|
235 | |||
236 | 3 | $this->flush(); |
|
237 | 3 | $this->conn->commit(); |
|
238 | |||
239 | 3 | return $return ?: true; |
|
240 | 1 | } catch (Throwable $e) { |
|
241 | 1 | $this->close(); |
|
242 | 1 | $this->conn->rollBack(); |
|
243 | |||
244 | 1 | throw $e; |
|
245 | } |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * {@inheritDoc} |
||
250 | */ |
||
251 | 1 | public function commit() |
|
252 | { |
||
253 | 1 | $this->conn->commit(); |
|
254 | 1 | } |
|
255 | |||
256 | /** |
||
257 | * {@inheritDoc} |
||
258 | */ |
||
259 | public function rollback() |
||
260 | { |
||
261 | $this->conn->rollBack(); |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Returns the ORM metadata descriptor for a class. |
||
266 | * |
||
267 | * The class name must be the fully-qualified class name without a leading backslash |
||
268 | * (as it is returned by get_class($obj)) or an aliased class name. |
||
269 | * |
||
270 | * Examples: |
||
271 | * MyProject\Domain\User |
||
272 | * sales:PriceRequest |
||
273 | * |
||
274 | * Internal note: Performance-sensitive method. |
||
275 | * |
||
276 | * @param string $className |
||
277 | * |
||
278 | * @return \Doctrine\ORM\Mapping\ClassMetadata |
||
279 | */ |
||
280 | 1990 | public function getClassMetadata($className) |
|
281 | { |
||
282 | 1990 | return $this->metadataFactory->getMetadataFor($className); |
|
283 | } |
||
284 | |||
285 | /** |
||
286 | * {@inheritDoc} |
||
287 | */ |
||
288 | 973 | public function createQuery($dql = '') |
|
289 | { |
||
290 | 973 | $query = new Query($this); |
|
291 | |||
292 | 973 | if ( ! empty($dql)) { |
|
293 | 968 | $query->setDQL($dql); |
|
294 | } |
||
295 | |||
296 | 973 | return $query; |
|
297 | } |
||
298 | |||
299 | /** |
||
300 | * {@inheritDoc} |
||
301 | */ |
||
302 | 1 | public function createNamedQuery($name) |
|
303 | { |
||
304 | 1 | return $this->createQuery($this->config->getNamedQuery($name)); |
|
305 | } |
||
306 | |||
307 | /** |
||
308 | * {@inheritDoc} |
||
309 | */ |
||
310 | 21 | public function createNativeQuery($sql, ResultSetMapping $rsm) |
|
311 | { |
||
312 | 21 | $query = new NativeQuery($this); |
|
313 | |||
314 | 21 | $query->setSQL($sql); |
|
315 | 21 | $query->setResultSetMapping($rsm); |
|
316 | |||
317 | 21 | return $query; |
|
318 | } |
||
319 | |||
320 | /** |
||
321 | * {@inheritDoc} |
||
322 | */ |
||
323 | 1 | public function createNamedNativeQuery($name) |
|
324 | { |
||
325 | 1 | list($sql, $rsm) = $this->config->getNamedNativeQuery($name); |
|
326 | |||
327 | 1 | return $this->createNativeQuery($sql, $rsm); |
|
328 | } |
||
329 | |||
330 | /** |
||
331 | * {@inheritDoc} |
||
332 | */ |
||
333 | 129 | public function createQueryBuilder() |
|
334 | { |
||
335 | 129 | return new QueryBuilder($this); |
|
336 | } |
||
337 | |||
338 | /** |
||
339 | * Flushes all changes to objects that have been queued up to now to the database. |
||
340 | * This effectively synchronizes the in-memory state of managed objects with the |
||
341 | * database. |
||
342 | * |
||
343 | * If an entity is explicitly passed to this method only this entity and |
||
344 | * the cascade-persist semantics + scheduled inserts/removals are synchronized. |
||
345 | * |
||
346 | * @param null|object|array $entity |
||
347 | * |
||
348 | * @return void |
||
349 | * |
||
350 | * @throws \Doctrine\ORM\OptimisticLockException If a version check on an entity that |
||
351 | * makes use of optimistic locking fails. |
||
352 | * @throws ORMException |
||
353 | */ |
||
354 | 1072 | public function flush($entity = null) |
|
355 | { |
||
356 | 1072 | $this->errorIfClosed(); |
|
357 | |||
358 | 1071 | $this->unitOfWork->commit($entity); |
|
359 | 1060 | } |
|
360 | |||
361 | /** |
||
362 | * Finds an Entity by its identifier. |
||
363 | * |
||
364 | * @param string $entityName The class name of the entity to find. |
||
365 | * @param mixed $id The identity of the entity to find. |
||
366 | * @param integer|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants |
||
367 | * or NULL if no specific lock mode should be used |
||
368 | * during the search. |
||
369 | * @param integer|null $lockVersion The version of the entity to find when using |
||
370 | * optimistic locking. |
||
371 | * |
||
372 | * @return object|null The entity instance or NULL if the entity can not be found. |
||
373 | * |
||
374 | * @throws OptimisticLockException |
||
375 | * @throws ORMInvalidArgumentException |
||
376 | * @throws TransactionRequiredException |
||
377 | * @throws ORMException |
||
378 | */ |
||
379 | 440 | public function find($entityName, $id, $lockMode = null, $lockVersion = null) |
|
380 | { |
||
381 | 440 | $class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\')); |
|
382 | |||
383 | 440 | if ( ! is_array($id)) { |
|
384 | 392 | if ($class->isIdentifierComposite) { |
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
385 | throw ORMInvalidArgumentException::invalidCompositeIdentifier(); |
||
386 | } |
||
387 | |||
388 | 392 | $id = [$class->identifier[0] => $id]; |
|
389 | } |
||
390 | |||
391 | 440 | foreach ($id as $i => $value) { |
|
392 | 440 | if (is_object($value) && $this->metadataFactory->hasMetadataFor(ClassUtils::getClass($value))) { |
|
393 | 10 | $id[$i] = $this->unitOfWork->getSingleIdentifierValue($value); |
|
394 | |||
395 | 10 | if ($id[$i] === null) { |
|
396 | 440 | throw ORMInvalidArgumentException::invalidIdentifierBindingEntity(); |
|
397 | } |
||
398 | } |
||
399 | } |
||
400 | |||
401 | 439 | $sortedId = []; |
|
402 | |||
403 | 439 | foreach ($class->identifier as $identifier) { |
|
404 | 439 | if ( ! isset($id[$identifier])) { |
|
405 | 1 | throw ORMException::missingIdentifierField($class->name, $identifier); |
|
0 ignored issues
–
show
|
|||
406 | } |
||
407 | |||
408 | 438 | $sortedId[$identifier] = $id[$identifier]; |
|
409 | 438 | unset($id[$identifier]); |
|
410 | } |
||
411 | |||
412 | 438 | if ($id) { |
|
413 | 1 | throw ORMException::unrecognizedIdentifierFields($class->name, array_keys($id)); |
|
414 | } |
||
415 | |||
416 | 437 | $unitOfWork = $this->getUnitOfWork(); |
|
417 | |||
418 | // Check identity map first |
||
419 | 437 | if (($entity = $unitOfWork->tryGetById($sortedId, $class->rootEntityName)) !== false) { |
|
0 ignored issues
–
show
|
|||
420 | 49 | if ( ! ($entity instanceof $class->name)) { |
|
421 | 1 | return null; |
|
422 | } |
||
423 | |||
424 | switch (true) { |
||
425 | 48 | case LockMode::OPTIMISTIC === $lockMode: |
|
426 | 1 | $this->lock($entity, $lockMode, $lockVersion); |
|
427 | break; |
||
428 | |||
429 | 48 | case LockMode::NONE === $lockMode: |
|
430 | 48 | case LockMode::PESSIMISTIC_READ === $lockMode: |
|
431 | 48 | case LockMode::PESSIMISTIC_WRITE === $lockMode: |
|
432 | $persister = $unitOfWork->getEntityPersister($class->name); |
||
433 | $persister->refresh($sortedId, $entity, $lockMode); |
||
434 | break; |
||
435 | } |
||
436 | |||
437 | 48 | return $entity; // Hit! |
|
438 | } |
||
439 | |||
440 | 415 | $persister = $unitOfWork->getEntityPersister($class->name); |
|
441 | |||
442 | switch (true) { |
||
443 | 415 | case LockMode::OPTIMISTIC === $lockMode: |
|
444 | 1 | if ( ! $class->isVersioned) { |
|
0 ignored issues
–
show
|
|||
445 | 1 | throw OptimisticLockException::notVersioned($class->name); |
|
446 | } |
||
447 | |||
448 | $entity = $persister->load($sortedId); |
||
449 | |||
450 | $unitOfWork->lock($entity, $lockMode, $lockVersion); |
||
451 | |||
452 | return $entity; |
||
453 | |||
454 | 414 | case LockMode::PESSIMISTIC_READ === $lockMode: |
|
455 | 413 | case LockMode::PESSIMISTIC_WRITE === $lockMode: |
|
456 | 2 | if ( ! $this->getConnection()->isTransactionActive()) { |
|
457 | 2 | throw TransactionRequiredException::transactionRequired(); |
|
458 | } |
||
459 | |||
460 | return $persister->load($sortedId, null, null, [], $lockMode); |
||
461 | |||
462 | default: |
||
463 | 412 | return $persister->loadById($sortedId); |
|
464 | } |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * {@inheritDoc} |
||
469 | */ |
||
470 | 94 | public function getReference($entityName, $id) |
|
471 | { |
||
472 | 94 | $class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\')); |
|
473 | |||
474 | 94 | if ( ! is_array($id)) { |
|
475 | 50 | $id = [$class->identifier[0] => $id]; |
|
476 | } |
||
477 | |||
478 | 94 | $sortedId = []; |
|
479 | |||
480 | 94 | foreach ($class->identifier as $identifier) { |
|
481 | 94 | if ( ! isset($id[$identifier])) { |
|
482 | throw ORMException::missingIdentifierField($class->name, $identifier); |
||
483 | } |
||
484 | |||
485 | 94 | $sortedId[$identifier] = $id[$identifier]; |
|
486 | 94 | unset($id[$identifier]); |
|
487 | } |
||
488 | |||
489 | 94 | if ($id) { |
|
490 | 1 | throw ORMException::unrecognizedIdentifierFields($class->name, array_keys($id)); |
|
491 | } |
||
492 | |||
493 | // Check identity map first, if its already in there just return it. |
||
494 | 93 | if (($entity = $this->unitOfWork->tryGetById($sortedId, $class->rootEntityName)) !== false) { |
|
495 | 28 | return ($entity instanceof $class->name) ? $entity : null; |
|
496 | } |
||
497 | |||
498 | 88 | if ($class->subClasses) { |
|
499 | 2 | return $this->find($entityName, $sortedId); |
|
500 | } |
||
501 | |||
502 | 88 | $entity = $this->proxyFactory->getProxy($class->name, $sortedId); |
|
503 | |||
504 | 88 | $this->unitOfWork->registerManaged($entity, $sortedId, []); |
|
505 | |||
506 | 88 | return $entity; |
|
507 | } |
||
508 | |||
509 | /** |
||
510 | * {@inheritDoc} |
||
511 | */ |
||
512 | 4 | public function getPartialReference($entityName, $identifier) |
|
513 | { |
||
514 | 4 | $class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\')); |
|
515 | |||
516 | // Check identity map first, if its already in there just return it. |
||
517 | 4 | if (($entity = $this->unitOfWork->tryGetById($identifier, $class->rootEntityName)) !== false) { |
|
518 | 1 | return ($entity instanceof $class->name) ? $entity : null; |
|
519 | } |
||
520 | |||
521 | 3 | if ( ! is_array($identifier)) { |
|
522 | 3 | $identifier = [$class->identifier[0] => $identifier]; |
|
523 | } |
||
524 | |||
525 | 3 | $entity = $class->newInstance(); |
|
526 | |||
527 | 3 | $class->setIdentifierValues($entity, $identifier); |
|
528 | |||
529 | 3 | $this->unitOfWork->registerManaged($entity, $identifier, []); |
|
530 | 3 | $this->unitOfWork->markReadOnly($entity); |
|
531 | |||
532 | 3 | return $entity; |
|
533 | } |
||
534 | |||
535 | /** |
||
536 | * Clears the EntityManager. All entities that are currently managed |
||
537 | * by this EntityManager become detached. |
||
538 | * |
||
539 | * @param string|null $entityName if given, only entities of this type will get detached |
||
540 | * |
||
541 | * @return void |
||
542 | * |
||
543 | * @throws ORMInvalidArgumentException if a non-null non-string value is given |
||
544 | * @throws \Doctrine\Common\Persistence\Mapping\MappingException if a $entityName is given, but that entity is not |
||
545 | * found in the mappings |
||
546 | */ |
||
547 | 1299 | public function clear($entityName = null) |
|
548 | { |
||
549 | 1299 | if (null !== $entityName && ! is_string($entityName)) { |
|
550 | 1 | throw ORMInvalidArgumentException::invalidEntityName($entityName); |
|
551 | } |
||
552 | |||
553 | 1298 | $this->unitOfWork->clear( |
|
554 | 1298 | null === $entityName |
|
555 | 1296 | ? null |
|
556 | 1298 | : $this->metadataFactory->getMetadataFor($entityName)->getName() |
|
557 | ); |
||
558 | 1297 | } |
|
559 | |||
560 | /** |
||
561 | * {@inheritDoc} |
||
562 | */ |
||
563 | 20 | public function close() |
|
564 | { |
||
565 | 20 | $this->clear(); |
|
566 | |||
567 | 20 | $this->closed = true; |
|
568 | 20 | } |
|
569 | |||
570 | /** |
||
571 | * Tells the EntityManager to make an instance managed and persistent. |
||
572 | * |
||
573 | * The entity will be entered into the database at or before transaction |
||
574 | * commit or as a result of the flush operation. |
||
575 | * |
||
576 | * NOTE: The persist operation always considers entities that are not yet known to |
||
577 | * this EntityManager as NEW. Do not pass detached entities to the persist operation. |
||
578 | * |
||
579 | * @param object $entity The instance to make managed and persistent. |
||
580 | * |
||
581 | * @return void |
||
582 | * |
||
583 | * @throws ORMInvalidArgumentException |
||
584 | * @throws ORMException |
||
585 | */ |
||
586 | 1068 | public function persist($entity) |
|
587 | { |
||
588 | 1068 | if ( ! is_object($entity)) { |
|
589 | 1 | throw ORMInvalidArgumentException::invalidObject('EntityManager#persist()', $entity); |
|
590 | } |
||
591 | |||
592 | 1067 | $this->errorIfClosed(); |
|
593 | |||
594 | 1066 | $this->unitOfWork->persist($entity); |
|
595 | 1065 | } |
|
596 | |||
597 | /** |
||
598 | * Removes an entity instance. |
||
599 | * |
||
600 | * A removed entity will be removed from the database at or before transaction commit |
||
601 | * or as a result of the flush operation. |
||
602 | * |
||
603 | * @param object $entity The entity instance to remove. |
||
604 | * |
||
605 | * @return void |
||
606 | * |
||
607 | * @throws ORMInvalidArgumentException |
||
608 | * @throws ORMException |
||
609 | */ |
||
610 | 52 | public function remove($entity) |
|
611 | { |
||
612 | 52 | if ( ! is_object($entity)) { |
|
613 | 1 | throw ORMInvalidArgumentException::invalidObject('EntityManager#remove()', $entity); |
|
614 | } |
||
615 | |||
616 | 51 | $this->errorIfClosed(); |
|
617 | |||
618 | 50 | $this->unitOfWork->remove($entity); |
|
619 | 50 | } |
|
620 | |||
621 | /** |
||
622 | * Refreshes the persistent state of an entity from the database, |
||
623 | * overriding any local changes that have not yet been persisted. |
||
624 | * |
||
625 | * @param object $entity The entity to refresh. |
||
626 | * |
||
627 | * @return void |
||
628 | * |
||
629 | * @throws ORMInvalidArgumentException |
||
630 | * @throws ORMException |
||
631 | */ |
||
632 | 19 | public function refresh($entity) |
|
633 | { |
||
634 | 19 | if ( ! is_object($entity)) { |
|
635 | 1 | throw ORMInvalidArgumentException::invalidObject('EntityManager#refresh()', $entity); |
|
636 | } |
||
637 | |||
638 | 18 | $this->errorIfClosed(); |
|
639 | |||
640 | 17 | $this->unitOfWork->refresh($entity); |
|
641 | 17 | } |
|
642 | |||
643 | /** |
||
644 | * Detaches an entity from the EntityManager, causing a managed entity to |
||
645 | * become detached. Unflushed changes made to the entity if any |
||
646 | * (including removal of the entity), will not be synchronized to the database. |
||
647 | * Entities which previously referenced the detached entity will continue to |
||
648 | * reference it. |
||
649 | * |
||
650 | * @param object $entity The entity to detach. |
||
651 | * |
||
652 | * @return void |
||
653 | * |
||
654 | * @throws ORMInvalidArgumentException |
||
655 | */ |
||
656 | 13 | public function detach($entity) |
|
657 | { |
||
658 | 13 | if ( ! is_object($entity)) { |
|
659 | 1 | throw ORMInvalidArgumentException::invalidObject('EntityManager#detach()', $entity); |
|
660 | } |
||
661 | |||
662 | 12 | $this->unitOfWork->detach($entity); |
|
663 | 12 | } |
|
664 | |||
665 | /** |
||
666 | * Merges the state of a detached entity into the persistence context |
||
667 | * of this EntityManager and returns the managed copy of the entity. |
||
668 | * The entity passed to merge will not become associated/managed with this EntityManager. |
||
669 | * |
||
670 | * @param object $entity The detached entity to merge into the persistence context. |
||
671 | * |
||
672 | * @return object The managed copy of the entity. |
||
673 | * |
||
674 | * @throws ORMInvalidArgumentException |
||
675 | * @throws ORMException |
||
676 | */ |
||
677 | 42 | public function merge($entity) |
|
678 | { |
||
679 | 42 | if ( ! is_object($entity)) { |
|
680 | 1 | throw ORMInvalidArgumentException::invalidObject('EntityManager#merge()', $entity); |
|
681 | } |
||
682 | |||
683 | 41 | $this->errorIfClosed(); |
|
684 | |||
685 | 40 | return $this->unitOfWork->merge($entity); |
|
686 | } |
||
687 | |||
688 | /** |
||
689 | * {@inheritDoc} |
||
690 | */ |
||
691 | public function copy($entity, $deep = false) |
||
692 | { |
||
693 | throw new \BadMethodCallException("Not implemented."); |
||
694 | } |
||
695 | |||
696 | /** |
||
697 | * {@inheritDoc} |
||
698 | */ |
||
699 | 9 | public function lock($entity, $lockMode, $lockVersion = null) |
|
700 | { |
||
701 | 9 | $this->unitOfWork->lock($entity, $lockMode, $lockVersion); |
|
702 | 2 | } |
|
703 | |||
704 | /** |
||
705 | * Gets the repository for an entity class. |
||
706 | * |
||
707 | * @param string $entityName The name of the entity. |
||
708 | * |
||
709 | * @return \Doctrine\Common\Persistence\ObjectRepository|\Doctrine\ORM\EntityRepository The repository class. |
||
710 | */ |
||
711 | 160 | public function getRepository($entityName) |
|
712 | { |
||
713 | 160 | return $this->repositoryFactory->getRepository($this, $entityName); |
|
714 | } |
||
715 | |||
716 | /** |
||
717 | * Determines whether an entity instance is managed in this EntityManager. |
||
718 | * |
||
719 | * @param object $entity |
||
720 | * |
||
721 | * @return boolean TRUE if this EntityManager currently manages the given entity, FALSE otherwise. |
||
722 | */ |
||
723 | 24 | public function contains($entity) |
|
724 | { |
||
725 | 24 | return $this->unitOfWork->isScheduledForInsert($entity) |
|
726 | 22 | || $this->unitOfWork->isInIdentityMap($entity) |
|
727 | 24 | && ! $this->unitOfWork->isScheduledForDelete($entity); |
|
728 | } |
||
729 | |||
730 | /** |
||
731 | * {@inheritDoc} |
||
732 | */ |
||
733 | 2463 | public function getEventManager() |
|
734 | { |
||
735 | 2463 | return $this->eventManager; |
|
736 | } |
||
737 | |||
738 | /** |
||
739 | * {@inheritDoc} |
||
740 | */ |
||
741 | 2463 | public function getConfiguration() |
|
742 | { |
||
743 | 2463 | return $this->config; |
|
744 | } |
||
745 | |||
746 | /** |
||
747 | * Throws an exception if the EntityManager is closed or currently not active. |
||
748 | * |
||
749 | * @return void |
||
750 | * |
||
751 | * @throws ORMException If the EntityManager is closed. |
||
752 | */ |
||
753 | 1087 | private function errorIfClosed() |
|
754 | { |
||
755 | 1087 | if ($this->closed) { |
|
756 | 5 | throw ORMException::entityManagerClosed(); |
|
757 | } |
||
758 | 1082 | } |
|
759 | |||
760 | /** |
||
761 | * {@inheritDoc} |
||
762 | */ |
||
763 | 2 | public function isOpen() |
|
764 | { |
||
765 | 2 | return (!$this->closed); |
|
766 | } |
||
767 | |||
768 | /** |
||
769 | * {@inheritDoc} |
||
770 | */ |
||
771 | 2463 | public function getUnitOfWork() |
|
772 | { |
||
773 | 2463 | return $this->unitOfWork; |
|
774 | } |
||
775 | |||
776 | /** |
||
777 | * {@inheritDoc} |
||
778 | */ |
||
779 | public function getHydrator($hydrationMode) |
||
780 | { |
||
781 | return $this->newHydrator($hydrationMode); |
||
782 | } |
||
783 | |||
784 | /** |
||
785 | * {@inheritDoc} |
||
786 | */ |
||
787 | 948 | public function newHydrator($hydrationMode) |
|
788 | { |
||
789 | switch ($hydrationMode) { |
||
790 | 948 | case Query::HYDRATE_OBJECT: |
|
791 | 652 | return new Internal\Hydration\ObjectHydrator($this); |
|
792 | |||
793 | 581 | case Query::HYDRATE_ARRAY: |
|
794 | 47 | return new Internal\Hydration\ArrayHydrator($this); |
|
795 | |||
796 | 539 | case Query::HYDRATE_SCALAR: |
|
797 | 85 | return new Internal\Hydration\ScalarHydrator($this); |
|
798 | |||
799 | 456 | case Query::HYDRATE_SINGLE_SCALAR: |
|
800 | 13 | return new Internal\Hydration\SingleScalarHydrator($this); |
|
801 | |||
802 | 447 | case Query::HYDRATE_SIMPLEOBJECT: |
|
803 | 446 | return new Internal\Hydration\SimpleObjectHydrator($this); |
|
804 | |||
805 | default: |
||
806 | 1 | if (($class = $this->config->getCustomHydrationMode($hydrationMode)) !== null) { |
|
807 | 1 | return new $class($this); |
|
808 | } |
||
809 | } |
||
810 | |||
811 | throw ORMException::invalidHydrationMode($hydrationMode); |
||
812 | } |
||
813 | |||
814 | /** |
||
815 | * {@inheritDoc} |
||
816 | */ |
||
817 | 177 | public function getProxyFactory() |
|
818 | { |
||
819 | 177 | return $this->proxyFactory; |
|
820 | } |
||
821 | |||
822 | /** |
||
823 | * {@inheritDoc} |
||
824 | */ |
||
825 | public function initializeObject($obj) |
||
826 | { |
||
827 | $this->unitOfWork->initializeObject($obj); |
||
828 | } |
||
829 | |||
830 | /** |
||
831 | * Factory method to create EntityManager instances. |
||
832 | * |
||
833 | * @param array|Connection $connection An array with the connection parameters or an existing Connection instance. |
||
834 | * @param Configuration $config The Configuration instance to use. |
||
835 | * @param EventManager $eventManager The EventManager instance to use. |
||
836 | * |
||
837 | * @return EntityManager The created EntityManager. |
||
838 | * |
||
839 | * @throws \InvalidArgumentException |
||
840 | * @throws ORMException |
||
841 | */ |
||
842 | 1301 | public static function create($connection, Configuration $config, EventManager $eventManager = null) |
|
843 | { |
||
844 | 1301 | if ( ! $config->getMetadataDriverImpl()) { |
|
845 | throw ORMException::missingMappingDriverImpl(); |
||
846 | } |
||
847 | |||
848 | 1301 | $connection = static::createConnection($connection, $config, $eventManager); |
|
849 | |||
850 | 1300 | return new EntityManager($connection, $config, $connection->getEventManager()); |
|
851 | } |
||
852 | |||
853 | /** |
||
854 | * Factory method to create Connection instances. |
||
855 | * |
||
856 | * @param array|Connection $connection An array with the connection parameters or an existing Connection instance. |
||
857 | * @param Configuration $config The Configuration instance to use. |
||
858 | * @param EventManager $eventManager The EventManager instance to use. |
||
859 | * |
||
860 | * @return Connection |
||
861 | * |
||
862 | * @throws \InvalidArgumentException |
||
863 | * @throws ORMException |
||
864 | */ |
||
865 | 1301 | protected static function createConnection($connection, Configuration $config, EventManager $eventManager = null) |
|
866 | { |
||
867 | 1301 | if (is_array($connection)) { |
|
868 | return DriverManager::getConnection($connection, $config, $eventManager ?: new EventManager()); |
||
869 | } |
||
870 | |||
871 | 1301 | if ( ! $connection instanceof Connection) { |
|
872 | 1 | throw new \InvalidArgumentException( |
|
873 | 1 | sprintf( |
|
874 | 1 | 'Invalid $connection argument of type %s given%s.', |
|
875 | 1 | is_object($connection) ? get_class($connection) : gettype($connection), |
|
876 | 1 | is_object($connection) ? '' : ': "' . $connection . '"' |
|
877 | ) |
||
878 | ); |
||
879 | } |
||
880 | |||
881 | 1300 | if ($eventManager !== null && $connection->getEventManager() !== $eventManager) { |
|
882 | throw ORMException::mismatchedEventManager(); |
||
883 | } |
||
884 | |||
885 | 1300 | return $connection; |
|
886 | } |
||
887 | |||
888 | /** |
||
889 | * {@inheritDoc} |
||
890 | */ |
||
891 | 639 | public function getFilters() |
|
892 | { |
||
893 | 639 | if (null === $this->filterCollection) { |
|
894 | 639 | $this->filterCollection = new FilterCollection($this); |
|
895 | } |
||
896 | |||
897 | 639 | return $this->filterCollection; |
|
898 | } |
||
899 | |||
900 | /** |
||
901 | * {@inheritDoc} |
||
902 | */ |
||
903 | 40 | public function isFiltersStateClean() |
|
904 | { |
||
905 | 40 | return null === $this->filterCollection || $this->filterCollection->isClean(); |
|
906 | } |
||
907 | |||
908 | /** |
||
909 | * {@inheritDoc} |
||
910 | */ |
||
911 | 779 | public function hasFilters() |
|
912 | { |
||
913 | 779 | return null !== $this->filterCollection; |
|
914 | } |
||
915 | } |
||
916 |