|
0
|
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 LGPL. For more information, see |
|
|
17 |
* <http://www.doctrine-project.org>. |
|
|
18 |
*/ |
|
|
19 |
|
|
|
20 |
namespace Doctrine\ORM\Internal\Hydration; |
|
|
21 |
|
|
|
22 |
use PDO, Doctrine\DBAL\Connection, Doctrine\ORM\Mapping\ClassMetadata; |
|
|
23 |
|
|
|
24 |
/** |
|
|
25 |
* The ArrayHydrator produces a nested array "graph" that is often (not always) |
|
|
26 |
* interchangeable with the corresponding object graph for read-only access. |
|
|
27 |
* |
|
|
28 |
* @author Roman Borschel <roman@code-factory.org> |
|
|
29 |
* @since 1.0 |
|
|
30 |
*/ |
|
|
31 |
class ArrayHydrator extends AbstractHydrator |
|
|
32 |
{ |
|
|
33 |
private $_ce = array(); |
|
|
34 |
private $_rootAliases = array(); |
|
|
35 |
private $_isSimpleQuery = false; |
|
|
36 |
private $_identifierMap = array(); |
|
|
37 |
private $_resultPointers = array(); |
|
|
38 |
private $_idTemplate = array(); |
|
|
39 |
private $_resultCounter = 0; |
|
|
40 |
|
|
|
41 |
/** @override */ |
|
|
42 |
protected function _prepare() |
|
|
43 |
{ |
|
|
44 |
$this->_isSimpleQuery = count($this->_rsm->aliasMap) <= 1; |
|
|
45 |
$this->_identifierMap = array(); |
|
|
46 |
$this->_resultPointers = array(); |
|
|
47 |
$this->_idTemplate = array(); |
|
|
48 |
$this->_resultCounter = 0; |
|
|
49 |
foreach ($this->_rsm->aliasMap as $dqlAlias => $className) { |
|
|
50 |
$this->_identifierMap[$dqlAlias] = array(); |
|
|
51 |
$this->_resultPointers[$dqlAlias] = array(); |
|
|
52 |
$this->_idTemplate[$dqlAlias] = ''; |
|
|
53 |
} |
|
|
54 |
} |
|
|
55 |
|
|
|
56 |
/** @override */ |
|
|
57 |
protected function _hydrateAll() |
|
|
58 |
{ |
|
|
59 |
$result = array(); |
|
|
60 |
$cache = array(); |
|
|
61 |
while ($data = $this->_stmt->fetch(PDO::FETCH_ASSOC)) { |
|
|
62 |
$this->_hydrateRow($data, $cache, $result); |
|
|
63 |
} |
|
|
64 |
|
|
|
65 |
return $result; |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
/** @override */ |
|
|
69 |
protected function _hydrateRow(array $data, array &$cache, array &$result) |
|
|
70 |
{ |
|
|
71 |
// 1) Initialize |
|
|
72 |
$id = $this->_idTemplate; // initialize the id-memory |
|
|
73 |
$nonemptyComponents = array(); |
|
|
74 |
$rowData = $this->_gatherRowData($data, $cache, $id, $nonemptyComponents); |
|
|
75 |
|
|
|
76 |
// Extract scalar values. They're appended at the end. |
|
|
77 |
if (isset($rowData['scalars'])) { |
|
|
78 |
$scalars = $rowData['scalars']; |
|
|
79 |
unset($rowData['scalars']); |
|
|
80 |
if (empty($rowData)) { |
|
|
81 |
++$this->_resultCounter; |
|
|
82 |
} |
|
|
83 |
} |
|
|
84 |
|
|
|
85 |
// 2) Now hydrate the data found in the current row. |
|
|
86 |
foreach ($rowData as $dqlAlias => $data) { |
|
|
87 |
$index = false; |
|
|
88 |
|
|
|
89 |
if (isset($this->_rsm->parentAliasMap[$dqlAlias])) { |
|
|
90 |
// It's a joined result |
|
|
91 |
|
|
|
92 |
$parent = $this->_rsm->parentAliasMap[$dqlAlias]; |
|
|
93 |
$path = $parent . '.' . $dqlAlias; |
|
|
94 |
|
|
|
95 |
// Get a reference to the right element in the result tree. |
|
|
96 |
// This element will get the associated element attached. |
|
|
97 |
if ($this->_rsm->isMixed && isset($this->_rootAliases[$parent])) { |
|
|
98 |
$first = reset($this->_resultPointers); |
|
|
99 |
// TODO: Exception if $key === null ? |
|
|
100 |
$baseElement =& $this->_resultPointers[$parent][key($first)]; |
|
|
101 |
} else if (isset($this->_resultPointers[$parent])) { |
|
|
102 |
$baseElement =& $this->_resultPointers[$parent]; |
|
|
103 |
} else { |
|
|
104 |
unset($this->_resultPointers[$dqlAlias]); // Ticket #1228 |
|
|
105 |
continue; |
|
|
106 |
} |
|
|
107 |
|
|
|
108 |
$relationAlias = $this->_rsm->relationMap[$dqlAlias]; |
|
|
109 |
$relation = $this->_getClassMetadata($this->_rsm->aliasMap[$parent])->associationMappings[$relationAlias]; |
|
|
110 |
|
|
|
111 |
// Check the type of the relation (many or single-valued) |
|
|
112 |
if ( ! ($relation['type'] & ClassMetadata::TO_ONE)) { |
|
|
113 |
$oneToOne = false; |
|
|
114 |
if (isset($nonemptyComponents[$dqlAlias])) { |
|
|
115 |
if ( ! isset($baseElement[$relationAlias])) { |
|
|
116 |
$baseElement[$relationAlias] = array(); |
|
|
117 |
} |
|
|
118 |
|
|
|
119 |
$indexExists = isset($this->_identifierMap[$path][$id[$parent]][$id[$dqlAlias]]); |
|
|
120 |
$index = $indexExists ? $this->_identifierMap[$path][$id[$parent]][$id[$dqlAlias]] : false; |
|
|
121 |
$indexIsValid = $index !== false ? isset($baseElement[$relationAlias][$index]) : false; |
|
|
122 |
|
|
|
123 |
if ( ! $indexExists || ! $indexIsValid) { |
|
|
124 |
$element = $data; |
|
|
125 |
if (isset($this->_rsm->indexByMap[$dqlAlias])) { |
|
|
126 |
$field = $this->_rsm->indexByMap[$dqlAlias]; |
|
|
127 |
$baseElement[$relationAlias][$element[$field]] = $element; |
|
|
128 |
} else { |
|
|
129 |
$baseElement[$relationAlias][] = $element; |
|
|
130 |
} |
|
|
131 |
end($baseElement[$relationAlias]); |
|
|
132 |
$this->_identifierMap[$path][$id[$parent]][$id[$dqlAlias]] = |
|
|
133 |
key($baseElement[$relationAlias]); |
|
|
134 |
} |
|
|
135 |
} else if ( ! isset($baseElement[$relationAlias])) { |
|
|
136 |
$baseElement[$relationAlias] = array(); |
|
|
137 |
} |
|
|
138 |
} else { |
|
|
139 |
$oneToOne = true; |
|
|
140 |
if ( ! isset($nonemptyComponents[$dqlAlias]) && ! isset($baseElement[$relationAlias])) { |
|
|
141 |
$baseElement[$relationAlias] = null; |
|
|
142 |
} else if ( ! isset($baseElement[$relationAlias])) { |
|
|
143 |
$baseElement[$relationAlias] = $data; |
|
|
144 |
} |
|
|
145 |
} |
|
|
146 |
|
|
|
147 |
$coll =& $baseElement[$relationAlias]; |
|
|
148 |
|
|
|
149 |
if ($coll !== null) { |
|
|
150 |
$this->updateResultPointer($coll, $index, $dqlAlias, $oneToOne); |
|
|
151 |
} |
|
|
152 |
|
|
|
153 |
} else { |
|
|
154 |
// It's a root result element |
|
|
155 |
|
|
|
156 |
$this->_rootAliases[$dqlAlias] = true; // Mark as root |
|
|
157 |
|
|
|
158 |
// Check for an existing element |
|
|
159 |
if ($this->_isSimpleQuery || ! isset($this->_identifierMap[$dqlAlias][$id[$dqlAlias]])) { |
|
|
160 |
$element = $rowData[$dqlAlias]; |
|
|
161 |
if (isset($this->_rsm->indexByMap[$dqlAlias])) { |
|
|
162 |
$field = $this->_rsm->indexByMap[$dqlAlias]; |
|
|
163 |
if ($this->_rsm->isMixed) { |
|
|
164 |
$result[] = array($element[$field] => $element); |
|
|
165 |
++$this->_resultCounter; |
|
|
166 |
} else { |
|
|
167 |
$result[$element[$field]] = $element; |
|
|
168 |
} |
|
|
169 |
} else { |
|
|
170 |
if ($this->_rsm->isMixed) { |
|
|
171 |
$result[] = array($element); |
|
|
172 |
++$this->_resultCounter; |
|
|
173 |
} else { |
|
|
174 |
$result[] = $element; |
|
|
175 |
} |
|
|
176 |
} |
|
|
177 |
end($result); |
|
|
178 |
$this->_identifierMap[$dqlAlias][$id[$dqlAlias]] = key($result); |
|
|
179 |
} else { |
|
|
180 |
$index = $this->_identifierMap[$dqlAlias][$id[$dqlAlias]]; |
|
|
181 |
/*if ($this->_rsm->isMixed) { |
|
|
182 |
$result[] =& $result[$index]; |
|
|
183 |
++$this->_resultCounter; |
|
|
184 |
}*/ |
|
|
185 |
} |
|
|
186 |
$this->updateResultPointer($result, $index, $dqlAlias, false); |
|
|
187 |
} |
|
|
188 |
} |
|
|
189 |
|
|
|
190 |
// Append scalar values to mixed result sets |
|
|
191 |
if (isset($scalars)) { |
|
|
192 |
foreach ($scalars as $name => $value) { |
|
|
193 |
$result[$this->_resultCounter - 1][$name] = $value; |
|
|
194 |
} |
|
|
195 |
} |
|
|
196 |
} |
|
|
197 |
|
|
|
198 |
/** |
|
|
199 |
* Updates the result pointer for an Entity. The result pointers point to the |
|
|
200 |
* last seen instance of each Entity type. This is used for graph construction. |
|
|
201 |
* |
|
|
202 |
* @param array $coll The element. |
|
|
203 |
* @param boolean|integer $index Index of the element in the collection. |
|
|
204 |
* @param string $dqlAlias |
|
|
205 |
* @param boolean $oneToOne Whether it is a single-valued association or not. |
|
|
206 |
*/ |
|
|
207 |
private function updateResultPointer(array &$coll, $index, $dqlAlias, $oneToOne) |
|
|
208 |
{ |
|
|
209 |
if ($coll === null) { |
|
|
210 |
unset($this->_resultPointers[$dqlAlias]); // Ticket #1228 |
|
|
211 |
return; |
|
|
212 |
} |
|
|
213 |
if ($index !== false) { |
|
|
214 |
$this->_resultPointers[$dqlAlias] =& $coll[$index]; |
|
|
215 |
return; |
|
|
216 |
} else { |
|
|
217 |
if ($coll) { |
|
|
218 |
if ($oneToOne) { |
|
|
219 |
$this->_resultPointers[$dqlAlias] =& $coll; |
|
|
220 |
} else { |
|
|
221 |
end($coll); |
|
|
222 |
$this->_resultPointers[$dqlAlias] =& $coll[key($coll)]; |
|
|
223 |
} |
|
|
224 |
} |
|
|
225 |
} |
|
|
226 |
} |
|
|
227 |
|
|
|
228 |
private function _getClassMetadata($className) |
|
|
229 |
{ |
|
|
230 |
if ( ! isset($this->_ce[$className])) { |
|
|
231 |
$this->_ce[$className] = $this->_em->getClassMetadata($className); |
|
|
232 |
} |
|
|
233 |
return $this->_ce[$className]; |
|
|
234 |
} |
|
|
235 |
} |