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.
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>.
20 namespace Doctrine\ORM\Internal\Hydration;
23 Doctrine\DBAL\Connection,
24 Doctrine\DBAL\Types\Type,
25 Doctrine\ORM\EntityManager,
27 Doctrine\ORM\Mapping\ClassMetadata;
30 * Base class for all hydrators. A hydrator is a class that provides some form
31 * of transformation of an SQL result set into another structure.
34 * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
35 * @author Roman Borschel <roman@code-factory.org>
36 * @author Guilherme Blanco <guilhermeblanoc@hotmail.com>
38 abstract class AbstractHydrator
40 /** @var \Doctrine\ORM\Query\ResultSetMapping The ResultSetMapping. */
43 /** @var EntityManager The EntityManager instance. */
46 /** @var \Doctrine\DBAL\Platforms\AbstractPlatform The dbms Platform instance */
49 /** @var \Doctrine\ORM\UnitOfWork The UnitOfWork of the associated EntityManager. */
52 /** @var array The cache used during row-by-row hydration. */
53 protected $_cache = array();
55 /** @var \Doctrine\DBAL\Driver\Statement The statement that provides the data to hydrate. */
58 /** @var array The query hints. */
62 * Initializes a new instance of a class derived from <tt>AbstractHydrator</tt>.
64 * @param \Doctrine\ORM\EntityManager $em The EntityManager to use.
66 public function __construct(EntityManager $em)
69 $this->_platform = $em->getConnection()->getDatabasePlatform();
70 $this->_uow = $em->getUnitOfWork();
74 * Initiates a row-by-row hydration.
77 * @param object $resultSetMapping
79 * @return IterableResult
81 public function iterate($stmt, $resultSetMapping, array $hints = array())
84 $this->_rsm = $resultSetMapping;
85 $this->_hints = $hints;
87 $evm = $this->_em->getEventManager();
88 $evm->addEventListener(array(Events::onClear), $this);
92 return new IterableResult($this);
96 * Hydrates all rows returned by the passed statement instance at once.
99 * @param object $resultSetMapping
100 * @param array $hints
103 public function hydrateAll($stmt, $resultSetMapping, array $hints = array())
105 $this->_stmt = $stmt;
106 $this->_rsm = $resultSetMapping;
107 $this->_hints = $hints;
111 $result = $this->hydrateAllData();
119 * Hydrates a single row returned by the current statement instance during
120 * row-by-row hydration with {@link iterate()}.
124 public function hydrateRow()
126 $row = $this->_stmt->fetch(PDO::FETCH_ASSOC);
136 $this->hydrateRowData($row, $this->_cache, $result);
142 * Excutes one-time preparation tasks, once each time hydration is started
143 * through {@link hydrateAll} or {@link iterate()}.
145 protected function prepare()
149 * Excutes one-time cleanup tasks at the end of a hydration that was initiated
150 * through {@link hydrateAll} or {@link iterate()}.
152 protected function cleanup()
156 $this->_stmt->closeCursor();
161 * Hydrates a single row from the current statement instance.
165 * @param array $data The row data.
166 * @param array $cache The cache to use.
167 * @param mixed $result The result to fill.
169 protected function hydrateRowData(array $data, array &$cache, array &$result)
171 throw new HydrationException("hydrateRowData() not implemented by this hydrator.");
175 * Hydrates all rows from the current statement instance at once.
177 abstract protected function hydrateAllData();
180 * Processes a row of the result set.
182 * Used for identity-based hydration (HYDRATE_OBJECT and HYDRATE_ARRAY).
183 * Puts the elements of a result row into a new array, grouped by the dql alias
184 * they belong to. The column names in the result set are mapped to their
185 * field names during this procedure as well as any necessary conversions on
186 * the values applied. Scalar values are kept in a specfic key 'scalars'.
188 * @param array $data SQL Result Row
189 * @param array &$cache Cache for column to field result information
190 * @param array &$id Dql-Alias => ID-Hash
191 * @param array &$nonemptyComponents Does this DQL-Alias has at least one non NULL value?
193 * @return array An array with all the fields (name => value) of the data row,
194 * grouped by their component alias.
196 protected function gatherRowData(array $data, array &$cache, array &$id, array &$nonemptyComponents)
200 foreach ($data as $key => $value) {
201 // Parse each column name only once. Cache the results.
202 if ( ! isset($cache[$key])) {
204 // NOTE: Most of the times it's a field mapping, so keep it first!!!
205 case (isset($this->_rsm->fieldMappings[$key])):
206 $fieldName = $this->_rsm->fieldMappings[$key];
207 $classMetadata = $this->_em->getClassMetadata($this->_rsm->declaringClasses[$key]);
209 $cache[$key]['fieldName'] = $fieldName;
210 $cache[$key]['type'] = Type::getType($classMetadata->fieldMappings[$fieldName]['type']);
211 $cache[$key]['isIdentifier'] = $classMetadata->isIdentifier($fieldName);
212 $cache[$key]['dqlAlias'] = $this->_rsm->columnOwnerMap[$key];
215 case (isset($this->_rsm->scalarMappings[$key])):
216 $cache[$key]['fieldName'] = $this->_rsm->scalarMappings[$key];
217 $cache[$key]['type'] = Type::getType($this->_rsm->typeMappings[$key]);
218 $cache[$key]['isScalar'] = true;
221 case (isset($this->_rsm->metaMappings[$key])):
222 // Meta column (has meaning in relational schema only, i.e. foreign keys or discriminator columns).
223 $fieldName = $this->_rsm->metaMappings[$key];
224 $classMetadata = $this->_em->getClassMetadata($this->_rsm->aliasMap[$this->_rsm->columnOwnerMap[$key]]);
226 $cache[$key]['isMetaColumn'] = true;
227 $cache[$key]['fieldName'] = $fieldName;
228 $cache[$key]['dqlAlias'] = $this->_rsm->columnOwnerMap[$key];
229 $cache[$key]['isIdentifier'] = isset($this->_rsm->isIdentifierColumn[$cache[$key]['dqlAlias']][$key]);
233 // this column is a left over, maybe from a LIMIT query hack for example in Oracle or DB2
234 // maybe from an additional column that has not been defined in a NativeQuery ResultSetMapping.
239 if (isset($cache[$key]['isScalar'])) {
240 $value = $cache[$key]['type']->convertToPHPValue($value, $this->_platform);
242 $rowData['scalars'][$cache[$key]['fieldName']] = $value;
247 $dqlAlias = $cache[$key]['dqlAlias'];
249 if ($cache[$key]['isIdentifier']) {
250 $id[$dqlAlias] .= '|' . $value;
253 if (isset($cache[$key]['isMetaColumn'])) {
254 if ( ! isset($rowData[$dqlAlias][$cache[$key]['fieldName']]) && $value !== null) {
255 $rowData[$dqlAlias][$cache[$key]['fieldName']] = $value;
256 if ($cache[$key]['isIdentifier']) {
257 $nonemptyComponents[$dqlAlias] = true;
264 // in an inheritance hierarchy the same field could be defined several times.
265 // We overwrite this value so long we dont have a non-null value, that value we keep.
266 // Per definition it cannot be that a field is defined several times and has several values.
267 if (isset($rowData[$dqlAlias][$cache[$key]['fieldName']]) && $value === null) {
271 $rowData[$dqlAlias][$cache[$key]['fieldName']] = $cache[$key]['type']->convertToPHPValue($value, $this->_platform);
273 if ( ! isset($nonemptyComponents[$dqlAlias]) && $value !== null) {
274 $nonemptyComponents[$dqlAlias] = true;
282 * Processes a row of the result set.
284 * Used for HYDRATE_SCALAR. This is a variant of _gatherRowData() that
285 * simply converts column names to field names and properly converts the
286 * values according to their types. The resulting row has the same number
287 * of elements as before.
290 * @param array $cache
292 * @return array The processed row.
294 protected function gatherScalarRowData(&$data, &$cache)
298 foreach ($data as $key => $value) {
299 // Parse each column name only once. Cache the results.
300 if ( ! isset($cache[$key])) {
302 // NOTE: During scalar hydration, most of the times it's a scalar mapping, keep it first!!!
303 case (isset($this->_rsm->scalarMappings[$key])):
304 $cache[$key]['fieldName'] = $this->_rsm->scalarMappings[$key];
305 $cache[$key]['isScalar'] = true;
308 case (isset($this->_rsm->fieldMappings[$key])):
309 $fieldName = $this->_rsm->fieldMappings[$key];
310 $classMetadata = $this->_em->getClassMetadata($this->_rsm->declaringClasses[$key]);
312 $cache[$key]['fieldName'] = $fieldName;
313 $cache[$key]['type'] = Type::getType($classMetadata->fieldMappings[$fieldName]['type']);
314 $cache[$key]['dqlAlias'] = $this->_rsm->columnOwnerMap[$key];
317 case (isset($this->_rsm->metaMappings[$key])):
318 // Meta column (has meaning in relational schema only, i.e. foreign keys or discriminator columns).
319 $cache[$key]['isMetaColumn'] = true;
320 $cache[$key]['fieldName'] = $this->_rsm->metaMappings[$key];
321 $cache[$key]['dqlAlias'] = $this->_rsm->columnOwnerMap[$key];
325 // this column is a left over, maybe from a LIMIT query hack for example in Oracle or DB2
326 // maybe from an additional column that has not been defined in a NativeQuery ResultSetMapping.
331 $fieldName = $cache[$key]['fieldName'];
334 case (isset($cache[$key]['isScalar'])):
335 $rowData[$fieldName] = $value;
338 case (isset($cache[$key]['isMetaColumn'])):
339 $rowData[$cache[$key]['dqlAlias'] . '_' . $fieldName] = $value;
343 $value = $cache[$key]['type']->convertToPHPValue($value, $this->_platform);
345 $rowData[$cache[$key]['dqlAlias'] . '_' . $fieldName] = $value;
353 * Register entity as managed in UnitOfWork.
355 * @param \Doctrine\ORM\Mapping\ClassMetadata $class
356 * @param object $entity
359 * @todo The "$id" generation is the same of UnitOfWork#createEntity. Remove this duplication somehow
361 protected function registerManaged(ClassMetadata $class, $entity, array $data)
363 if ($class->isIdentifierComposite) {
365 foreach ($class->identifier as $fieldName) {
366 if (isset($class->associationMappings[$fieldName])) {
367 $id[$fieldName] = $data[$class->associationMappings[$fieldName]['joinColumns'][0]['name']];
369 $id[$fieldName] = $data[$fieldName];
373 if (isset($class->associationMappings[$class->identifier[0]])) {
374 $id = array($class->identifier[0] => $data[$class->associationMappings[$class->identifier[0]]['joinColumns'][0]['name']]);
376 $id = array($class->identifier[0] => $data[$class->identifier[0]]);
380 $this->_em->getUnitOfWork()->registerManaged($entity, $id, $data);
384 * When executed in a hydrate() loop we have to clear internal state to
385 * decrease memory consumption.
387 public function onClear($eventArgs)