|
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\Common\Util\ClassUtils; |
|
24
|
|
|
|
|
25
|
|
|
use Doctrine\ORM\Query; |
|
26
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
27
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
28
|
|
|
use Doctrine\ORM\Utility\IdentifierFlattener; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Default hydrator cache for entities |
|
32
|
|
|
* |
|
33
|
|
|
* @since 2.5 |
|
34
|
|
|
* @author Fabio B. Silva <[email protected]> |
|
35
|
|
|
*/ |
|
36
|
|
|
class DefaultEntityHydrator implements EntityHydrator |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* @var \Doctrine\ORM\EntityManagerInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $em; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var \Doctrine\ORM\UnitOfWork |
|
45
|
|
|
*/ |
|
46
|
|
|
private $uow; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The IdentifierFlattener used for manipulating identifiers |
|
50
|
|
|
* |
|
51
|
|
|
* @var \Doctrine\ORM\Utility\IdentifierFlattener |
|
52
|
|
|
*/ |
|
53
|
|
|
private $identifierFlattener; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var array |
|
57
|
|
|
*/ |
|
58
|
|
|
private static $hints = [Query::HINT_CACHE_ENABLED => true]; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param \Doctrine\ORM\EntityManagerInterface $em The entity manager. |
|
62
|
|
|
*/ |
|
63
|
183 |
|
public function __construct(EntityManagerInterface $em) |
|
64
|
|
|
{ |
|
65
|
183 |
|
$this->em = $em; |
|
66
|
183 |
|
$this->uow = $em->getUnitOfWork(); |
|
67
|
183 |
|
$this->identifierFlattener = new IdentifierFlattener($em->getUnitOfWork(), $em->getMetadataFactory()); |
|
68
|
183 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
84 |
|
public function buildCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, $entity) |
|
74
|
|
|
{ |
|
75
|
84 |
|
$data = $this->uow->getOriginalEntityData($entity); |
|
76
|
84 |
|
$data = array_merge($data, $metadata->getIdentifierValues($entity)); // why update has no identifier values ? |
|
77
|
|
|
|
|
78
|
84 |
|
if ($metadata->isVersioned) { |
|
79
|
1 |
|
$data[$metadata->versionField] = $metadata->getFieldValue($entity, $metadata->versionField); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
84 |
|
foreach ($metadata->associationMappings as $name => $assoc) { |
|
83
|
44 |
|
if ( ! isset($data[$name])) { |
|
84
|
23 |
|
continue; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
43 |
|
if ( ! ($assoc['type'] & ClassMetadata::TO_ONE)) { |
|
88
|
33 |
|
unset($data[$name]); |
|
89
|
|
|
|
|
90
|
33 |
|
continue; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
42 |
|
if ( ! isset($assoc['cache'])) { |
|
94
|
5 |
|
$targetClassMetadata = $this->em->getClassMetadata($assoc['targetEntity']); |
|
95
|
5 |
|
$owningAssociation = ( ! $assoc['isOwningSide']) |
|
96
|
1 |
|
? $targetClassMetadata->associationMappings[$assoc['mappedBy']] |
|
|
|
|
|
|
97
|
5 |
|
: $assoc; |
|
98
|
5 |
|
$associationIds = $this->identifierFlattener->flattenIdentifier( |
|
99
|
5 |
|
$targetClassMetadata, |
|
100
|
5 |
|
$targetClassMetadata->getIdentifierValues($data[$name]) |
|
101
|
|
|
); |
|
102
|
|
|
|
|
103
|
5 |
|
unset($data[$name]); |
|
104
|
|
|
|
|
105
|
5 |
|
foreach ($associationIds as $fieldName => $fieldValue) { |
|
106
|
5 |
|
if (isset($targetClassMetadata->fieldMappings[$fieldName])) { |
|
|
|
|
|
|
107
|
5 |
|
$fieldMapping = $targetClassMetadata->fieldMappings[$fieldName]; |
|
108
|
|
|
|
|
109
|
5 |
|
$data[$owningAssociation['targetToSourceKeyColumns'][$fieldMapping['columnName']]] = $fieldValue; |
|
110
|
|
|
|
|
111
|
5 |
|
continue; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
1 |
|
$targetAssoc = $targetClassMetadata->associationMappings[$fieldName]; |
|
115
|
|
|
|
|
116
|
1 |
|
foreach($assoc['targetToSourceKeyColumns'] as $referencedColumn => $localColumn) { |
|
117
|
1 |
|
if (isset($targetAssoc['sourceToTargetKeyColumns'][$referencedColumn])) { |
|
118
|
1 |
|
$data[$localColumn] = $fieldValue; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
5 |
|
continue; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
38 |
|
if ( ! isset($assoc['id'])) { |
|
127
|
38 |
|
$targetClass = ClassUtils::getClass($data[$name]); |
|
128
|
38 |
|
$targetId = $this->uow->getEntityIdentifier($data[$name]); |
|
129
|
38 |
|
$data[$name] = new AssociationCacheEntry($targetClass, $targetId); |
|
130
|
|
|
|
|
131
|
38 |
|
continue; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
// handle association identifier |
|
135
|
|
|
$targetId = is_object($data[$name]) && $this->uow->isInIdentityMap($data[$name]) |
|
136
|
|
|
? $this->uow->getEntityIdentifier($data[$name]) |
|
137
|
|
|
: $data[$name]; |
|
138
|
|
|
|
|
139
|
|
|
// @TODO - fix it ! |
|
140
|
|
|
// handle UnitOfWork#createEntity hash generation |
|
141
|
|
|
if ( ! is_array($targetId)) { |
|
142
|
|
|
$data[reset($assoc['joinColumnFieldNames'])] = $targetId; |
|
143
|
|
|
|
|
144
|
|
|
$targetEntity = $this->em->getClassMetadata($assoc['targetEntity']); |
|
145
|
|
|
$targetId = [$targetEntity->identifier[0] => $targetId]; |
|
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
$data[$name] = new AssociationCacheEntry($assoc['targetEntity'], $targetId); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
84 |
|
return new EntityCacheEntry($metadata->name, $data); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* {@inheritdoc} |
|
156
|
|
|
*/ |
|
157
|
16 |
|
public function loadCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, EntityCacheEntry $entry, $entity = null) |
|
158
|
|
|
{ |
|
159
|
16 |
|
$data = $entry->data; |
|
160
|
16 |
|
$hints = self::$hints; |
|
161
|
|
|
|
|
162
|
16 |
|
if ($entity !== null) { |
|
163
|
2 |
|
$hints[Query::HINT_REFRESH] = true; |
|
164
|
2 |
|
$hints[Query::HINT_REFRESH_ENTITY] = $entity; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
16 |
|
foreach ($metadata->associationMappings as $name => $assoc) { |
|
168
|
12 |
|
if ( ! isset($assoc['cache']) || ! isset($data[$name])) { |
|
169
|
10 |
|
continue; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
7 |
|
$assocClass = $data[$name]->class; |
|
173
|
7 |
|
$assocId = $data[$name]->identifier; |
|
174
|
7 |
|
$isEagerLoad = ($assoc['fetch'] === ClassMetadata::FETCH_EAGER || ($assoc['type'] === ClassMetadata::ONE_TO_ONE && ! $assoc['isOwningSide'])); |
|
175
|
|
|
|
|
176
|
7 |
|
if ( ! $isEagerLoad) { |
|
177
|
6 |
|
$data[$name] = $this->em->getReference($assocClass, $assocId); |
|
178
|
|
|
|
|
179
|
6 |
|
continue; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
1 |
|
$assocMetadata = $this->em->getClassMetadata($assoc['targetEntity']); |
|
183
|
1 |
|
$assocKey = new EntityCacheKey($assocMetadata->rootEntityName, $assocId); |
|
|
|
|
|
|
184
|
1 |
|
$assocPersister = $this->uow->getEntityPersister($assoc['targetEntity']); |
|
185
|
1 |
|
$assocRegion = $assocPersister->getCacheRegion(); |
|
|
|
|
|
|
186
|
1 |
|
$assocEntry = $assocRegion->get($assocKey); |
|
187
|
|
|
|
|
188
|
1 |
|
if ($assocEntry === null) { |
|
189
|
|
|
return null; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
1 |
|
$data[$name] = $this->uow->createEntity($assocEntry->class, $assocEntry->resolveAssociationEntries($this->em), $hints); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
16 |
|
if ($entity !== null) { |
|
196
|
2 |
|
$this->uow->registerManaged($entity, $key->identifier, $data); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
16 |
|
$result = $this->uow->createEntity($entry->class, $data, $hints); |
|
200
|
|
|
|
|
201
|
16 |
|
$this->uow->hydrationComplete(); |
|
202
|
|
|
|
|
203
|
16 |
|
return $result; |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|