Completed
Push — master ( 2a239b...205ee7 )
by Marco
22s
created

lib/Doctrine/ORM/EntityManager.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 2318
    protected function __construct(Connection $conn, Configuration $config, EventManager $eventManager)
150
    {
151 2318
        $this->conn              = $conn;
152 2318
        $this->config            = $config;
153 2318
        $this->eventManager      = $eventManager;
154
155 2318
        $metadataFactoryClassName = $config->getClassMetadataFactoryName();
156
157 2318
        $this->metadataFactory = new $metadataFactoryClassName;
158 2318
        $this->metadataFactory->setEntityManager($this);
159 2318
        $this->metadataFactory->setCacheDriver($this->config->getMetadataCacheImpl());
160
161 2318
        $this->repositoryFactory = $config->getRepositoryFactory();
162 2318
        $this->unitOfWork        = new UnitOfWork($this);
163 2318
        $this->proxyFactory      = new ProxyFactory(
164
            $this,
165 2318
            $config->getProxyDir(),
166 2318
            $config->getProxyNamespace(),
167 2318
            $config->getAutoGenerateProxyClasses()
168
        );
169
170 2318
        if ($config->isSecondLevelCacheEnabled()) {
171 282
            $cacheConfig    = $config->getSecondLevelCacheConfiguration();
172 282
            $cacheFactory   = $cacheConfig->getCacheFactory();
173 282
            $this->cache    = $cacheFactory->createCache($this);
174
        }
175 2318
    }
176
177
    /**
178
     * {@inheritDoc}
179
     */
180 1808
    public function getConnection()
181
    {
182 1808
        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 2318
    public function getMetadataFactory()
191
    {
192 2318
        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 1
    public function beginTransaction()
211
    {
212 1
        $this->conn->beginTransaction();
213 1
    }
214
215
    /**
216
     * {@inheritDoc}
217
     */
218 215
    public function getCache()
219
    {
220 215
        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
    public function rollback()
261
    {
262
        $this->conn->rollBack();
263
    }
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 1883
    public function getClassMetadata($className)
282
    {
283 1883
        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 20
    public function createNativeQuery($sql, ResultSetMapping $rsm)
312
    {
313 20
        $query = new NativeQuery($this);
314
315 20
        $query->setSQL($sql);
316 20
        $query->setResultSetMapping($rsm);
317
318 20
        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 115
    public function createQueryBuilder()
335
    {
336 115
        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 997
    public function flush($entity = null)
356
    {
357 997
        $this->errorIfClosed();
358
359 996
        $this->unitOfWork->commit($entity);
360 989
    }
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 412
    public function find($entityName, $id, $lockMode = null, $lockVersion = null)
381
    {
382 412
        $class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\'));
383
384 412
        if ( ! is_array($id)) {
385 368
            if ($class->isIdentifierComposite) {
386
                throw ORMInvalidArgumentException::invalidCompositeIdentifier();
387
            }
388
389 368
            $id = [$class->identifier[0] => $id];
390
        }
391
392 412
        foreach ($id as $i => $value) {
393 412
            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 412
                    throw ORMInvalidArgumentException::invalidIdentifierBindingEntity();
398
                }
399
            }
400
        }
401
402 411
        $sortedId = [];
403
404 411
        foreach ($class->identifier as $identifier) {
405 411
            if ( ! isset($id[$identifier])) {
406 1
                throw ORMException::missingIdentifierField($class->name, $identifier);
407
            }
408
409 410
            $sortedId[$identifier] = $id[$identifier];
410 410
            unset($id[$identifier]);
411
        }
412
413 410
        if ($id) {
414 1
            throw ORMException::unrecognizedIdentifierFields($class->name, array_keys($id));
415
        }
416
417 409
        $unitOfWork = $this->getUnitOfWork();
418
419
        // Check identity map first
420 409
        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 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, [], $lockMode);
462
463
            default:
464 390
                return $persister->loadById($sortedId);
465
        }
466
    }
467
468
    /**
469
     * {@inheritDoc}
470
     */
471 90
    public function getReference($entityName, $id)
472
    {
473 90
        $class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\'));
474
475 90
        if ( ! is_array($id)) {
476 47
            $id = [$class->identifier[0] => $id];
477
        }
478
479 90
        $sortedId = [];
480
481 90
        foreach ($class->identifier as $identifier) {
482 90
            if ( ! isset($id[$identifier])) {
483
                throw ORMException::missingIdentifierField($class->name, $identifier);
484
            }
485
486 90
            $sortedId[$identifier] = $id[$identifier];
487 90
            unset($id[$identifier]);
488
        }
489
490 90
        if ($id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
491 1
            throw ORMException::unrecognizedIdentifierFields($class->name, array_keys($id));
0 ignored issues
show
Accessing name on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
492
        }
493
494
        // Check identity map first, if its already in there just return it.
495 89
        if (($entity = $this->unitOfWork->tryGetById($sortedId, $class->rootEntityName)) !== false) {
496 27
            return ($entity instanceof $class->name) ? $entity : null;
497
        }
498
499 85
        if ($class->subClasses) {
500 2
            return $this->find($entityName, $sortedId);
501
        }
502
503 85
        $entity = $this->proxyFactory->getProxy($class->name, $sortedId);
504
505 85
        $this->unitOfWork->registerManaged($entity, $sortedId, []);
506
507 85
        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 = [$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, []);
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 1206
    public function clear($entityName = null)
549
    {
550 1206
        if (null !== $entityName && ! is_string($entityName)) {
551 1
            throw ORMInvalidArgumentException::invalidEntityName($entityName);
552
        }
553
554 1205
        $this->unitOfWork->clear(
555 1205
            null === $entityName
556 1203
                ? null
557 1205
                : $this->metadataFactory->getMetadataFor($entityName)->getName()
558
        );
559 1204
    }
560
561
    /**
562
     * {@inheritDoc}
563
     */
564 18
    public function close()
565
    {
566 18
        $this->clear();
567
568 18
        $this->closed = true;
569 18
    }
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 993
    public function persist($entity)
588
    {
589 993
        if ( ! is_object($entity)) {
590 1
            throw ORMInvalidArgumentException::invalidObject('EntityManager#persist()', $entity);
591
        }
592
593 992
        $this->errorIfClosed();
594
595 991
        $this->unitOfWork->persist($entity);
596 990
    }
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 42
    public function remove($entity)
612
    {
613 42
        if ( ! is_object($entity)) {
614 1
            throw ORMInvalidArgumentException::invalidObject('EntityManager#remove()', $entity);
615
        }
616
617 41
        $this->errorIfClosed();
618
619 40
        $this->unitOfWork->remove($entity);
620 40
    }
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 18
    public function refresh($entity)
634
    {
635 18
        if ( ! is_object($entity)) {
636 1
            throw ORMInvalidArgumentException::invalidObject('EntityManager#refresh()', $entity);
637
        }
638
639 17
        $this->errorIfClosed();
640
641 16
        $this->unitOfWork->refresh($entity);
642 16
    }
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 12
    public function detach($entity)
658
    {
659 12
        if ( ! is_object($entity)) {
660 1
            throw ORMInvalidArgumentException::invalidObject('EntityManager#detach()', $entity);
661
        }
662
663 11
        $this->unitOfWork->detach($entity);
664 11
    }
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 41
    public function merge($entity)
679
    {
680 41
        if ( ! is_object($entity)) {
681 1
            throw ORMInvalidArgumentException::invalidObject('EntityManager#merge()', $entity);
682
        }
683
684 40
        $this->errorIfClosed();
685
686 39
        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 7
    public function lock($entity, $lockMode, $lockVersion = null)
704
    {
705 7
        $this->unitOfWork->lock($entity, $lockMode, $lockVersion);
706
    }
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 149
    public function getRepository($entityName)
716
    {
717 149
        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 2318
    public function getEventManager()
738
    {
739 2318
        return $this->eventManager;
740
    }
741
742
    /**
743
     * {@inheritDoc}
744
     */
745 2318
    public function getConfiguration()
746
    {
747 2318
        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 1011
    private function errorIfClosed()
758
    {
759 1011
        if ($this->closed) {
760 5
            throw ORMException::entityManagerClosed();
761
        }
762 1006
    }
763
764
    /**
765
     * {@inheritDoc}
766
     */
767 1
    public function isOpen()
768
    {
769 1
        return (!$this->closed);
770
    }
771
772
    /**
773
     * {@inheritDoc}
774
     */
775 2318
    public function getUnitOfWork()
776
    {
777 2318
        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 883
    public function newHydrator($hydrationMode)
792
    {
793
        switch ($hydrationMode) {
794 883
            case Query::HYDRATE_OBJECT:
795 615
                return new Internal\Hydration\ObjectHydrator($this);
796
797 541
            case Query::HYDRATE_ARRAY:
798 36
                return new Internal\Hydration\ArrayHydrator($this);
799
800 510
            case Query::HYDRATE_SCALAR:
801 85
                return new Internal\Hydration\ScalarHydrator($this);
802
803 427
            case Query::HYDRATE_SINGLE_SCALAR:
804 12
                return new Internal\Hydration\SingleScalarHydrator($this);
805
806 419
            case Query::HYDRATE_SIMPLEOBJECT:
807 418
                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 172
    public function getProxyFactory()
822
    {
823 172
        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 1209
    public static function create($connection, Configuration $config, EventManager $eventManager = null)
847
    {
848 1209
        if ( ! $config->getMetadataDriverImpl()) {
849
            throw ORMException::missingMappingDriverImpl();
850
        }
851
852 1209
        $connection = static::createConnection($connection, $config, $eventManager);
853
854 1208
        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 1209
    protected static function createConnection($connection, Configuration $config, EventManager $eventManager = null)
870
    {
871 1209
        if (is_array($connection)) {
872
            return DriverManager::getConnection($connection, $config, $eventManager ?: new EventManager());
873
        }
874
875 1209
        if ( ! $connection instanceof Connection) {
876 1
            throw new \InvalidArgumentException(
877 1
                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 1208
        if ($eventManager !== null && $connection->getEventManager() !== $eventManager) {
886
            throw ORMException::mismatchedEventManager();
887
        }
888
889 1208
        return $connection;
890
    }
891
892
    /**
893
     * {@inheritDoc}
894
     */
895 609
    public function getFilters()
896
    {
897 609
        if (null === $this->filterCollection) {
898 609
            $this->filterCollection = new FilterCollection($this);
899
        }
900
901 609
        return $this->filterCollection;
902
    }
903
904
    /**
905
     * {@inheritDoc}
906
     */
907 40
    public function isFiltersStateClean()
908
    {
909 40
        return null === $this->filterCollection || $this->filterCollection->isClean();
910
    }
911
912
    /**
913
     * {@inheritDoc}
914
     */
915 736
    public function hasFilters()
916
    {
917 736
        return null !== $this->filterCollection;
918
    }
919
}
920