2008-05-06 17:41:22 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.phpdoctrine.org>.
|
|
|
|
*/
|
|
|
|
|
2008-12-18 17:08:11 +03:00
|
|
|
#namespace Doctrine\ORM\Internal\Hydration;
|
2008-05-30 16:09:24 +04:00
|
|
|
|
2008-05-06 17:41:22 +04:00
|
|
|
/**
|
2009-01-03 22:50:13 +03:00
|
|
|
* The hydrator has the tedious task to process result sets returned by the database
|
2008-05-08 18:17:35 +04:00
|
|
|
* and turn them into useable structures.
|
|
|
|
*
|
|
|
|
* Runtime complexity: The following gives the overall number of iterations
|
2008-05-18 00:04:56 +04:00
|
|
|
* required to process a result set when using identity hydration
|
|
|
|
* (HYDRATE_IDENTITY_OBJECT or HYDRATE_IDENTITY_ARRAY).
|
2008-05-08 18:17:35 +04:00
|
|
|
*
|
|
|
|
* <code>numRowsInResult * numColumnsInResult + numRowsInResult * numClassesInQuery</code>
|
|
|
|
*
|
|
|
|
* This comes down to:
|
|
|
|
*
|
|
|
|
* <code>(numRowsInResult * (numColumnsInResult + numClassesInQuery))</code>
|
|
|
|
*
|
2008-05-18 00:04:56 +04:00
|
|
|
* For scalar hydration (HYDRATE_SCALAR) it's:
|
|
|
|
*
|
|
|
|
* <code>numRowsInResult * numColumnsInResult</code>
|
|
|
|
*
|
|
|
|
* Note that this is only a crude definition as it also heavily
|
2008-05-08 18:17:35 +04:00
|
|
|
* depends on the complexity of all the single operations that are performed in
|
|
|
|
* each iteration.
|
|
|
|
*
|
|
|
|
* As can be seen, the number of columns in the result has the most impact on
|
2008-05-18 00:04:56 +04:00
|
|
|
* the overall performance (apart from the row count, of course), since numClassesInQuery
|
2008-05-08 18:17:35 +04:00
|
|
|
* is usually pretty low.
|
2008-05-18 00:04:56 +04:00
|
|
|
* That's why the performance of the _gatherRowData() methods which are responsible
|
|
|
|
* for the "numRowsInResult * numColumnsInResult" part is crucial to fast hydration.
|
2008-05-06 17:41:22 +04:00
|
|
|
*
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
2009-01-14 00:56:43 +03:00
|
|
|
* @link www.doctrine-project.org
|
2008-05-06 17:41:22 +04:00
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision: 3192 $
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
*/
|
2008-09-12 13:58:02 +04:00
|
|
|
class Doctrine_ORM_Internal_Hydration_StandardHydrator extends Doctrine_ORM_Internal_Hydration_AbstractHydrator
|
2008-05-06 17:41:22 +04:00
|
|
|
{
|
|
|
|
/**
|
2008-05-18 00:04:56 +04:00
|
|
|
* Parses the data returned by statement object.
|
2008-05-06 17:41:22 +04:00
|
|
|
*
|
|
|
|
* This is method defines the core of Doctrine's object population algorithm.
|
|
|
|
*
|
|
|
|
* @param array $aliasMap Array that maps DQL aliases to their components
|
|
|
|
* (DQL alias => array(
|
2008-06-15 19:56:28 +04:00
|
|
|
* 'metadata' => Table object,
|
2008-05-06 17:41:22 +04:00
|
|
|
* 'parent' => Parent DQL alias (if any),
|
|
|
|
* 'relation' => Relation object (if any),
|
|
|
|
* 'map' => Custom index to use as the key in the result (if any),
|
|
|
|
* 'agg' => List of aggregate value names (sql alias => dql alias)
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* @return mixed The created object/array graph.
|
2008-05-18 20:21:55 +04:00
|
|
|
* @throws Doctrine_Hydrator_Exception If the hydration process failed.
|
2008-05-06 17:41:22 +04:00
|
|
|
*/
|
2008-05-14 01:20:34 +04:00
|
|
|
public function hydrateResultSet($parserResult)
|
2008-05-06 17:41:22 +04:00
|
|
|
{
|
2008-05-14 01:20:34 +04:00
|
|
|
if ($parserResult->getHydrationMode() === null) {
|
2008-05-06 17:41:22 +04:00
|
|
|
$hydrationMode = $this->_hydrationMode;
|
2008-05-14 01:20:34 +04:00
|
|
|
} else {
|
|
|
|
$hydrationMode = $parserResult->getHydrationMode();
|
2008-05-06 17:41:22 +04:00
|
|
|
}
|
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
$stmt = $parserResult->getDatabaseStatement();
|
|
|
|
|
2008-09-23 06:45:02 +04:00
|
|
|
if ($hydrationMode == Doctrine_ORM_Query::HYDRATE_NONE) {
|
2008-05-06 17:41:22 +04:00
|
|
|
return $stmt->fetchAll(PDO::FETCH_NUM);
|
|
|
|
}
|
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
$this->_tableAliases = $parserResult->getTableToClassAliasMap();
|
|
|
|
$this->_queryComponents = $parserResult->getQueryComponents();
|
2008-05-06 17:41:22 +04:00
|
|
|
|
2008-09-23 06:45:02 +04:00
|
|
|
if ($hydrationMode == Doctrine_ORM_Query::HYDRATE_ARRAY) {
|
2008-09-12 13:58:02 +04:00
|
|
|
$driver = new Doctrine_ORM_Internal_Hydration_ArrayDriver();
|
2008-05-06 17:41:22 +04:00
|
|
|
} else {
|
2008-09-12 13:58:02 +04:00
|
|
|
$driver = new Doctrine_ORM_Internal_Hydration_ObjectDriver($this->_em);
|
2008-05-06 17:41:22 +04:00
|
|
|
}
|
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
$s = microtime(true);
|
2008-05-06 17:41:22 +04:00
|
|
|
|
|
|
|
reset($this->_queryComponents);
|
|
|
|
$rootAlias = key($this->_queryComponents);
|
2008-07-11 14:48:04 +04:00
|
|
|
$rootEntityName = $this->_queryComponents[$rootAlias]['metadata']->getClassName();
|
2008-05-06 17:41:22 +04:00
|
|
|
// if only one class is involved we can make our lives easier
|
|
|
|
$isSimpleQuery = count($this->_queryComponents) <= 1;
|
2009-01-12 16:34:41 +03:00
|
|
|
// Lookup map to quickly discover/lookup existing entities in the result
|
2008-05-06 17:41:22 +04:00
|
|
|
// It's the identifier "memory"
|
|
|
|
$identifierMap = array();
|
|
|
|
// Holds for each class a pointer to the last previously seen element in the result set
|
|
|
|
$resultPointers = array();
|
2009-01-12 16:34:41 +03:00
|
|
|
// Holds the values of the identifier/primary key fields of entities,
|
|
|
|
// separated by a pipe '|' and grouped by DQL class alias (r, u, i, ... whatever)
|
|
|
|
// The $idTemplate is a prepared template. $id is set to a fresh template when
|
2008-05-06 17:41:22 +04:00
|
|
|
// starting to process a row.
|
|
|
|
$id = array();
|
|
|
|
$idTemplate = array();
|
|
|
|
|
2008-09-23 06:45:02 +04:00
|
|
|
if ($parserResult->isMixedQuery() || $hydrationMode == Doctrine_ORM_Query::HYDRATE_SCALAR) {
|
2008-05-06 17:41:22 +04:00
|
|
|
$result = array();
|
|
|
|
} else {
|
2008-07-11 14:48:04 +04:00
|
|
|
$result = $driver->getElementCollection($rootEntityName);
|
2008-05-06 17:41:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt === false || $stmt === 0) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize
|
|
|
|
foreach ($this->_queryComponents as $dqlAlias => $component) {
|
|
|
|
$identifierMap[$dqlAlias] = array();
|
|
|
|
$resultPointers[$dqlAlias] = array();
|
|
|
|
$idTemplate[$dqlAlias] = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$cache = array();
|
2008-05-18 20:21:55 +04:00
|
|
|
// Evaluate HYDRATE_SINGLE_SCALAR
|
2008-09-23 06:45:02 +04:00
|
|
|
if ($hydrationMode == Doctrine_ORM_Query::HYDRATE_SINGLE_SCALAR) {
|
2008-05-18 20:21:55 +04:00
|
|
|
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
2008-12-18 17:08:11 +03:00
|
|
|
//TODO: Let this exception be raised by Query as QueryException
|
2008-05-18 20:21:55 +04:00
|
|
|
if (count($result) > 1 || count($result[0]) > 1) {
|
2008-09-12 13:58:02 +04:00
|
|
|
throw Doctrine_ORM_Exceptions_HydrationException::nonUniqueResult();
|
2008-05-18 20:21:55 +04:00
|
|
|
}
|
2008-09-14 00:06:49 +04:00
|
|
|
$result = $this->_gatherScalarRowData($result[0], $cache);
|
|
|
|
return array_shift($result);
|
2008-05-18 20:21:55 +04:00
|
|
|
}
|
2009-01-03 22:50:13 +03:00
|
|
|
|
|
|
|
$resultCounter = 0;
|
2008-05-18 20:21:55 +04:00
|
|
|
// Process result set
|
2008-09-13 16:40:17 +04:00
|
|
|
while ($data = $stmt->fetch(PDO::FETCH_ASSOC)) {
|
2008-05-18 20:21:55 +04:00
|
|
|
// Evaluate HYDRATE_SCALAR
|
2008-09-23 06:45:02 +04:00
|
|
|
if ($hydrationMode == Doctrine_ORM_Query::HYDRATE_SCALAR) {
|
2008-05-18 00:04:56 +04:00
|
|
|
$result[] = $this->_gatherScalarRowData($data, $cache);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-07-11 14:48:04 +04:00
|
|
|
// From here on its all about graph construction
|
|
|
|
|
|
|
|
// 1) Initialize
|
2008-05-06 17:41:22 +04:00
|
|
|
$id = $idTemplate; // initialize the id-memory
|
|
|
|
$nonemptyComponents = array();
|
|
|
|
$rowData = $this->_gatherRowData($data, $cache, $id, $nonemptyComponents);
|
|
|
|
|
2008-07-27 23:38:56 +04:00
|
|
|
// 2) Hydrate the data of the root entity from the current row
|
2008-05-06 17:41:22 +04:00
|
|
|
// Check for an existing element
|
|
|
|
$index = false;
|
|
|
|
if ($isSimpleQuery || ! isset($identifierMap[$rootAlias][$id[$rootAlias]])) {
|
2008-07-11 14:48:04 +04:00
|
|
|
$element = $driver->getElement($rowData[$rootAlias], $rootEntityName);
|
2008-05-06 17:41:22 +04:00
|
|
|
if ($field = $this->_getCustomIndexField($rootAlias)) {
|
2008-05-14 01:20:34 +04:00
|
|
|
if ($parserResult->isMixedQuery()) {
|
|
|
|
$result[] = array(
|
2008-06-15 19:56:28 +04:00
|
|
|
$driver->getFieldValue($element, $field) => $element
|
|
|
|
);
|
2009-01-03 22:50:13 +03:00
|
|
|
++$resultCounter;
|
2008-05-06 17:41:22 +04:00
|
|
|
} else {
|
2008-05-14 01:20:34 +04:00
|
|
|
$driver->addElementToIndexedCollection($result, $element, $field);
|
2008-05-06 17:41:22 +04:00
|
|
|
}
|
|
|
|
} else {
|
2008-05-14 01:20:34 +04:00
|
|
|
if ($parserResult->isMixedQuery()) {
|
2008-05-06 17:41:22 +04:00
|
|
|
$result[] = array($element);
|
2009-01-03 22:50:13 +03:00
|
|
|
++$resultCounter;
|
2008-05-06 17:41:22 +04:00
|
|
|
} else {
|
2008-05-14 01:20:34 +04:00
|
|
|
$driver->addElementToCollection($result, $element);
|
2008-05-06 17:41:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$identifierMap[$rootAlias][$id[$rootAlias]] = $driver->getLastKey($result);
|
|
|
|
} else {
|
|
|
|
$index = $identifierMap[$rootAlias][$id[$rootAlias]];
|
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
$driver->updateResultPointer($resultPointers, $result, $index, $rootAlias, false);
|
2008-05-06 17:41:22 +04:00
|
|
|
unset($rowData[$rootAlias]);
|
|
|
|
// end hydrate data of the root component for the current row
|
|
|
|
|
2008-07-11 14:48:04 +04:00
|
|
|
// Extract scalar values. They're appended at the end.
|
2008-05-06 17:41:22 +04:00
|
|
|
if (isset($rowData['scalars'])) {
|
|
|
|
$scalars = $rowData['scalars'];
|
|
|
|
unset($rowData['scalars']);
|
|
|
|
}
|
|
|
|
|
2008-07-11 14:48:04 +04:00
|
|
|
// 3) Now hydrate the rest of the data found in the current row, that
|
2008-07-27 23:38:56 +04:00
|
|
|
// belongs to other (related) entities.
|
2008-05-06 17:41:22 +04:00
|
|
|
foreach ($rowData as $dqlAlias => $data) {
|
|
|
|
$index = false;
|
|
|
|
$map = $this->_queryComponents[$dqlAlias];
|
2008-07-11 14:48:04 +04:00
|
|
|
$entityName = $map['metadata']->getClassName();
|
2008-05-06 17:41:22 +04:00
|
|
|
$parent = $map['parent'];
|
|
|
|
$relation = $map['relation'];
|
2008-12-18 17:08:11 +03:00
|
|
|
$relationAlias = $relation->getSourceFieldName();
|
2008-05-06 17:41:22 +04:00
|
|
|
$path = $parent . '.' . $dqlAlias;
|
|
|
|
|
2008-12-18 17:08:11 +03:00
|
|
|
// Get a reference to the right element in the result tree.
|
|
|
|
// This element will get the associated element attached.
|
2008-05-14 01:20:34 +04:00
|
|
|
if ($parserResult->isMixedQuery() && $parent == $rootAlias) {
|
2008-05-08 18:17:35 +04:00
|
|
|
$key = key(reset($resultPointers));
|
|
|
|
// TODO: Exception if $key === null ?
|
2008-05-06 17:41:22 +04:00
|
|
|
$baseElement =& $resultPointers[$parent][$key];
|
|
|
|
} else if (isset($resultPointers[$parent])) {
|
|
|
|
$baseElement =& $resultPointers[$parent];
|
|
|
|
} else {
|
2008-07-11 14:48:04 +04:00
|
|
|
unset($resultPointers[$dqlAlias]); // Ticket #1228
|
2008-05-06 17:41:22 +04:00
|
|
|
continue;
|
|
|
|
}
|
2008-05-08 18:17:35 +04:00
|
|
|
|
2009-01-03 22:50:13 +03:00
|
|
|
// Check the type of the relation (many or single-valued)
|
2008-05-06 17:41:22 +04:00
|
|
|
if ( ! $relation->isOneToOne()) {
|
2008-12-18 17:08:11 +03:00
|
|
|
// x-to-many relation
|
2008-05-06 17:41:22 +04:00
|
|
|
$oneToOne = false;
|
|
|
|
if (isset($nonemptyComponents[$dqlAlias])) {
|
2008-05-08 18:17:35 +04:00
|
|
|
$driver->initRelatedCollection($baseElement, $relationAlias);
|
2008-05-24 21:10:45 +04:00
|
|
|
$indexExists = isset($identifierMap[$path][$id[$parent]][$id[$dqlAlias]]);
|
|
|
|
$index = $indexExists ? $identifierMap[$path][$id[$parent]][$id[$dqlAlias]] : false;
|
2008-12-18 17:08:11 +03:00
|
|
|
$indexIsValid = $index !== false ? $driver->isIndexKeyInUse($baseElement, $relationAlias, $index) : false;
|
2008-05-24 21:10:45 +04:00
|
|
|
if ( ! $indexExists || ! $indexIsValid) {
|
2008-07-11 14:48:04 +04:00
|
|
|
$element = $driver->getElement($data, $entityName);
|
2008-05-06 17:41:22 +04:00
|
|
|
if ($field = $this->_getCustomIndexField($dqlAlias)) {
|
2008-05-14 01:20:34 +04:00
|
|
|
$driver->addRelatedIndexedElement($baseElement, $relationAlias, $element, $field);
|
2008-05-06 17:41:22 +04:00
|
|
|
} else {
|
2008-05-14 01:20:34 +04:00
|
|
|
$driver->addRelatedElement($baseElement, $relationAlias, $element);
|
2008-05-06 17:41:22 +04:00
|
|
|
}
|
2008-05-14 01:20:34 +04:00
|
|
|
$identifierMap[$path][$id[$parent]][$id[$dqlAlias]] = $driver->getLastKey(
|
|
|
|
$driver->getReferenceValue($baseElement, $relationAlias));
|
2008-05-06 17:41:22 +04:00
|
|
|
}
|
2008-12-18 17:08:11 +03:00
|
|
|
} else if ( ! $driver->isFieldSet($baseElement, $relationAlias)) {
|
2008-09-23 06:45:02 +04:00
|
|
|
if ($hydrationMode == Doctrine_ORM_Query::HYDRATE_ARRAY) {
|
2008-09-14 00:29:44 +04:00
|
|
|
$baseElement[$relationAlias] = array();
|
2008-09-14 00:06:49 +04:00
|
|
|
} else {
|
|
|
|
$driver->setRelatedElement($baseElement, $relationAlias,
|
|
|
|
$driver->getElementCollection($entityName));
|
|
|
|
}
|
2008-05-06 17:41:22 +04:00
|
|
|
}
|
|
|
|
} else {
|
2008-12-18 17:08:11 +03:00
|
|
|
// x-to-one relation
|
2008-05-08 18:17:35 +04:00
|
|
|
$oneToOne = true;
|
2008-07-10 21:17:58 +04:00
|
|
|
if ( ! isset($nonemptyComponents[$dqlAlias]) &&
|
|
|
|
! $driver->isFieldSet($baseElement, $relationAlias)) {
|
2008-05-14 01:20:34 +04:00
|
|
|
$driver->setRelatedElement($baseElement, $relationAlias,
|
|
|
|
$driver->getNullPointer());
|
|
|
|
} else if ( ! $driver->isFieldSet($baseElement, $relationAlias)) {
|
|
|
|
$driver->setRelatedElement($baseElement, $relationAlias,
|
2008-07-11 14:48:04 +04:00
|
|
|
$driver->getElement($data, $entityName));
|
2008-05-06 17:41:22 +04:00
|
|
|
}
|
|
|
|
}
|
2008-09-14 00:12:08 +04:00
|
|
|
|
2008-09-23 06:45:02 +04:00
|
|
|
if ($hydrationMode == Doctrine_ORM_Query::HYDRATE_ARRAY) {
|
2008-09-14 00:29:44 +04:00
|
|
|
$coll =& $baseElement[$relationAlias];
|
2008-09-14 00:06:49 +04:00
|
|
|
} else {
|
2008-12-18 17:08:11 +03:00
|
|
|
$coll = $driver->getReferenceValue($baseElement, $relationAlias);
|
2008-09-14 00:12:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($coll !== null) {
|
2008-12-18 17:08:11 +03:00
|
|
|
$driver->updateResultPointer($resultPointers, $coll, $index, $dqlAlias, $oneToOne);
|
2008-05-08 18:17:35 +04:00
|
|
|
}
|
2008-05-06 17:41:22 +04:00
|
|
|
}
|
|
|
|
|
2008-12-18 17:08:11 +03:00
|
|
|
// Append scalar values to mixed result sets
|
2008-05-06 17:41:22 +04:00
|
|
|
if (isset($scalars)) {
|
|
|
|
foreach ($scalars as $name => $value) {
|
2009-01-03 22:50:13 +03:00
|
|
|
$result[$resultCounter - 1][$name] = $value;
|
2008-05-06 17:41:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->closeCursor();
|
|
|
|
$driver->flush();
|
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
$e = microtime(true);
|
2008-12-18 17:08:11 +03:00
|
|
|
echo 'Hydration took: ' . ($e - $s) . PHP_EOL;
|
2008-05-06 17:41:22 +04:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|