|
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 = array(Query::HINT_CACHE_ENABLED => true); |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param \Doctrine\ORM\EntityManagerInterface $em The entity manager. |
|
53
|
|
|
*/ |
|
54
|
116 |
|
public function __construct(EntityManagerInterface $em) |
|
55
|
|
|
{ |
|
56
|
116 |
|
$this->em = $em; |
|
57
|
116 |
|
$this->uow = $em->getUnitOfWork(); |
|
58
|
116 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
*/ |
|
63
|
42 |
|
public function buildCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key, $collection) |
|
64
|
|
|
{ |
|
65
|
42 |
|
$data = array(); |
|
66
|
|
|
|
|
67
|
42 |
|
foreach ($collection as $index => $entity) { |
|
68
|
42 |
|
$data[$index] = new EntityCacheKey($metadata->name, $this->uow->getEntityIdentifier($entity)); |
|
69
|
|
|
} |
|
70
|
42 |
|
return new CollectionCacheEntry($data); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
16 |
|
public function loadCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key, CollectionCacheEntry $entry, PersistentCollection $collection) |
|
77
|
|
|
{ |
|
78
|
16 |
|
$assoc = $metadata->associationMappings[$key->association]; |
|
79
|
|
|
/* @var $targetPersister \Doctrine\ORM\Cache\Persister\CachedPersister */ |
|
80
|
16 |
|
$targetPersister = $this->uow->getEntityPersister($assoc['targetEntity']); |
|
81
|
16 |
|
$targetRegion = $targetPersister->getCacheRegion(); |
|
82
|
16 |
|
$list = array(); |
|
83
|
|
|
|
|
84
|
16 |
|
$entityEntries = $targetRegion->getMultiple($entry); |
|
85
|
|
|
|
|
86
|
16 |
|
if ($entityEntries === null) { |
|
87
|
|
|
return null; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/* @var $entityEntries \Doctrine\ORM\Cache\EntityCacheEntry[] */ |
|
91
|
16 |
|
foreach ($entityEntries as $index => $entityEntry) { |
|
92
|
14 |
|
$list[$index] = $this->uow->createEntity($entityEntry->class, $entityEntry->resolveAssociationEntries($this->em), self::$hints); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
16 |
|
array_walk($list, function($entity, $index) use ($collection) { |
|
96
|
14 |
|
$collection->hydrateSet($index, $entity); |
|
97
|
16 |
|
}); |
|
98
|
|
|
|
|
99
|
16 |
|
$this->uow->hydrationComplete(); |
|
100
|
|
|
|
|
101
|
16 |
|
return $list; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|