|
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\Internal\Hydration; |
|
21
|
|
|
|
|
22
|
|
|
use PDO; |
|
23
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
24
|
|
|
use Doctrine\ORM\Query; |
|
25
|
|
|
|
|
26
|
|
|
class SimpleObjectHydrator extends AbstractHydrator |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var ClassMetadata |
|
30
|
|
|
*/ |
|
31
|
|
|
private $class; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
454 |
|
protected function prepare() |
|
37
|
|
|
{ |
|
38
|
454 |
|
if (count($this->_rsm->aliasMap) !== 1) { |
|
39
|
|
|
throw new \RuntimeException("Cannot use SimpleObjectHydrator with a ResultSetMapping that contains more than one object result."); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
454 |
|
if ($this->_rsm->scalarMappings) { |
|
|
|
|
|
|
43
|
|
|
throw new \RuntimeException("Cannot use SimpleObjectHydrator with a ResultSetMapping that contains scalar mappings."); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
454 |
|
$this->class = $this->getClassMetadata(reset($this->_rsm->aliasMap)); |
|
47
|
454 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
451 |
|
protected function cleanup() |
|
53
|
|
|
{ |
|
54
|
451 |
|
parent::cleanup(); |
|
55
|
|
|
|
|
56
|
451 |
|
$this->_uow->triggerEagerLoads(); |
|
57
|
451 |
|
$this->_uow->hydrationComplete(); |
|
58
|
451 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
*/ |
|
63
|
453 |
|
protected function hydrateAllData() |
|
64
|
|
|
{ |
|
65
|
453 |
|
$result = []; |
|
66
|
|
|
|
|
67
|
453 |
|
while ($row = $this->_stmt->fetch(PDO::FETCH_ASSOC)) { |
|
68
|
391 |
|
$this->hydrateRowData($row, $result); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
451 |
|
$this->_em->getUnitOfWork()->triggerEagerLoads(); |
|
72
|
|
|
|
|
73
|
451 |
|
return $result; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
392 |
|
protected function hydrateRowData(array $sqlResult, array &$result) |
|
80
|
|
|
{ |
|
81
|
392 |
|
$entityName = $this->class->name; |
|
82
|
392 |
|
$data = []; |
|
83
|
|
|
|
|
84
|
|
|
// We need to find the correct entity class name if we have inheritance in resultset |
|
85
|
392 |
|
if ($this->class->inheritanceType !== ClassMetadata::INHERITANCE_TYPE_NONE) { |
|
86
|
82 |
|
$discrColumnName = $this->_platform->getSQLResultCasing($this->class->discriminatorColumn['name']); |
|
87
|
|
|
|
|
88
|
|
|
// Find mapped discriminator column from the result set. |
|
89
|
82 |
|
if ($metaMappingDiscrColumnName = array_search($discrColumnName, $this->_rsm->metaMappings)) { |
|
90
|
82 |
|
$discrColumnName = $metaMappingDiscrColumnName; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
82 |
|
if ( ! isset($sqlResult[$discrColumnName])) { |
|
94
|
1 |
|
throw HydrationException::missingDiscriminatorColumn($entityName, $discrColumnName, key($this->_rsm->aliasMap)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
81 |
|
if ($sqlResult[$discrColumnName] === '') { |
|
98
|
|
|
throw HydrationException::emptyDiscriminatorValue(key($this->_rsm->aliasMap)); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
81 |
|
$discrMap = $this->class->discriminatorMap; |
|
102
|
|
|
|
|
103
|
81 |
|
if ( ! isset($discrMap[$sqlResult[$discrColumnName]])) { |
|
104
|
1 |
|
throw HydrationException::invalidDiscriminatorValue($sqlResult[$discrColumnName], array_keys($discrMap)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
80 |
|
$this->class->discriminatorValue = $sqlResult[$discrColumnName]; |
|
108
|
|
|
|
|
109
|
80 |
|
$entityName = $discrMap[$sqlResult[$discrColumnName]]; |
|
110
|
|
|
|
|
111
|
80 |
|
unset($sqlResult[$discrColumnName]); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
390 |
|
foreach ($sqlResult as $column => $value) { |
|
115
|
|
|
// An ObjectHydrator should be used instead of SimpleObjectHydrator |
|
116
|
390 |
|
if (isset($this->_rsm->relationMap[$column])) { |
|
117
|
|
|
throw new \Exception(sprintf('Unable to retrieve association information for column "%s"', $column)); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
390 |
|
$cacheKeyInfo = $this->hydrateColumnInfo($column); |
|
121
|
|
|
|
|
122
|
390 |
|
if ( ! $cacheKeyInfo || ($this->class->discriminatorValue && |
|
123
|
390 |
|
!$cacheKeyInfo['isIdentifier'] && |
|
124
|
390 |
|
isset($cacheKeyInfo['discriminatorValue']) && |
|
125
|
390 |
|
$this->class->discriminatorValue !== $cacheKeyInfo['discriminatorValue'] && |
|
126
|
390 |
|
!in_array($this->class->discriminatorValue, $cacheKeyInfo['discriminatorValues'])) |
|
127
|
|
|
) { |
|
128
|
21 |
|
continue; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
// Check if value is null before conversion (because some types convert null to something else) |
|
132
|
390 |
|
$valueIsNull = null === $value; |
|
133
|
|
|
|
|
134
|
|
|
// Convert field to a valid PHP value |
|
135
|
390 |
|
if (isset($cacheKeyInfo['type'])) { |
|
136
|
390 |
|
$type = $cacheKeyInfo['type']; |
|
137
|
390 |
|
$value = $type->convertToPHPValue($value, $this->_platform); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
390 |
|
$fieldName = $cacheKeyInfo['fieldName']; |
|
141
|
|
|
|
|
142
|
|
|
// Prevent overwrite in case of inherit classes using same property name (See AbstractHydrator) |
|
143
|
390 |
|
if ( ! isset($data[$fieldName]) || ! $valueIsNull) { |
|
144
|
390 |
|
$data[$fieldName] = $value; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
390 |
|
if (isset($this->_hints[Query::HINT_REFRESH_ENTITY])) { |
|
149
|
48 |
|
$this->registerManaged($this->class, $this->_hints[Query::HINT_REFRESH_ENTITY], $data); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
390 |
|
$uow = $this->_em->getUnitOfWork(); |
|
153
|
390 |
|
$entity = $uow->createEntity($entityName, $data, $this->_hints); |
|
154
|
|
|
|
|
155
|
390 |
|
$result[] = $entity; |
|
156
|
|
|
|
|
157
|
390 |
|
if (isset($this->_hints[Query::HINT_INTERNAL_ITERATION]) && $this->_hints[Query::HINT_INTERNAL_ITERATION]) { |
|
158
|
1 |
|
$this->_uow->hydrationComplete(); |
|
159
|
|
|
} |
|
160
|
390 |
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
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.