1 | <?php |
||
2 | |||
3 | /* |
||
4 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||
5 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||
6 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||
7 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||
8 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||
9 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||
10 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
11 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
12 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
13 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||
14 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
15 | * |
||
16 | * This software consists of voluntary contributions made by many individuals |
||
17 | * and is licensed under the MIT license. For more information, see |
||
18 | * <http://www.doctrine-project.org>. |
||
19 | */ |
||
20 | |||
21 | namespace Doctrine\ORM\Cache; |
||
22 | |||
23 | use Doctrine\ORM\Query; |
||
24 | use Doctrine\ORM\PersistentCollection; |
||
25 | use Doctrine\ORM\Mapping\ClassMetadata; |
||
26 | use Doctrine\ORM\EntityManagerInterface; |
||
27 | |||
28 | /** |
||
29 | * Default hydrator cache for collections |
||
30 | * |
||
31 | * @since 2.5 |
||
32 | * @author Fabio B. Silva <[email protected]> |
||
33 | */ |
||
34 | class DefaultCollectionHydrator implements CollectionHydrator |
||
35 | { |
||
36 | /** |
||
37 | * @var \Doctrine\ORM\EntityManagerInterface |
||
38 | */ |
||
39 | private $em; |
||
40 | |||
41 | /** |
||
42 | * @var \Doctrine\ORM\UnitOfWork |
||
43 | */ |
||
44 | private $uow; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | private static $hints = [Query::HINT_CACHE_ENABLED => true]; |
||
50 | |||
51 | /** |
||
52 | * @param \Doctrine\ORM\EntityManagerInterface $em The entity manager. |
||
53 | */ |
||
54 | 119 | public function __construct(EntityManagerInterface $em) |
|
55 | { |
||
56 | 119 | $this->em = $em; |
|
57 | 119 | $this->uow = $em->getUnitOfWork(); |
|
58 | 119 | } |
|
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | 45 | public function buildCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key, $collection) |
|
64 | { |
||
65 | 45 | $data = []; |
|
66 | |||
67 | 45 | foreach ($collection as $index => $entity) { |
|
68 | 45 | $data[$index] = new EntityCacheKey($metadata->rootEntityName, $this->uow->getEntityIdentifier($entity)); |
|
69 | } |
||
70 | |||
71 | 45 | return new CollectionCacheEntry($data); |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | 16 | public function loadCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key, CollectionCacheEntry $entry, PersistentCollection $collection) |
|
78 | { |
||
79 | 16 | $assoc = $metadata->associationMappings[$key->association]; |
|
80 | /* @var $targetPersister \Doctrine\ORM\Cache\Persister\CachedPersister */ |
||
81 | 16 | $targetPersister = $this->uow->getEntityPersister($assoc['targetEntity']); |
|
82 | 16 | $targetRegion = $targetPersister->getCacheRegion(); |
|
83 | 16 | $list = []; |
|
84 | |||
85 | 16 | $entityEntries = $targetRegion->getMultiple($entry); |
|
86 | |||
87 | 16 | if ($entityEntries === null) { |
|
88 | 1 | return null; |
|
89 | } |
||
90 | |||
91 | /* @var $entityEntries \Doctrine\ORM\Cache\EntityCacheEntry[] */ |
||
92 | 16 | foreach ($entityEntries as $index => $entityEntry) { |
|
93 | 14 | $list[$index] = $this->uow->createEntity($entityEntry->class, $entityEntry->resolveAssociationEntries($this->em), self::$hints); |
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
94 | } |
||
95 | |||
96 | 16 | array_walk($list, function($entity, $index) use ($collection) { |
|
97 | 14 | $collection->hydrateSet($index, $entity); |
|
98 | 16 | }); |
|
99 | |||
100 | 16 | $this->uow->hydrationComplete(); |
|
101 | |||
102 | 16 | return $list; |
|
103 | } |
||
104 | } |
||
105 |