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-05-30 16:09:24 +04:00
|
|
|
#namespace Doctrine::ORM::Internal;
|
|
|
|
|
2008-05-06 17:41:22 +04:00
|
|
|
/**
|
2008-05-08 18:17:35 +04:00
|
|
|
* The hydrator has the tedious to process result sets returned by the database
|
|
|
|
* 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
|
|
|
*
|
|
|
|
* @package Doctrine
|
|
|
|
* @subpackage Hydrator
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link www.phpdoctrine.org
|
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision: 3192 $
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
*/
|
|
|
|
class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract
|
|
|
|
{
|
|
|
|
/**
|
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.
|
|
|
|
*
|
|
|
|
* @todo: Detailed documentation. Refactor (too long & nesting level).
|
|
|
|
*
|
|
|
|
* @param mixed $stmt
|
|
|
|
* @param array $tableAliases Array that maps table aliases (SQL alias => DQL alias)
|
|
|
|
* @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-05-06 17:41:22 +04:00
|
|
|
if ($hydrationMode == Doctrine::HYDRATE_NONE) {
|
|
|
|
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
|
|
|
|
|
|
|
if ($hydrationMode == Doctrine::HYDRATE_ARRAY) {
|
|
|
|
$driver = new Doctrine_Hydrator_ArrayDriver();
|
|
|
|
} else {
|
2008-05-14 01:20:34 +04:00
|
|
|
$driver = new Doctrine_Hydrator_RecordDriver($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
|
|
|
|
|
|
|
// Used variables during hydration
|
|
|
|
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;
|
|
|
|
// Lookup map to quickly discover/lookup existing records in the result
|
|
|
|
// 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();
|
|
|
|
// holds the values of the identifier/primary key fields of components,
|
|
|
|
// separated by a pipe '|' and grouped by component alias (r, u, i, ... whatever)
|
|
|
|
// the $idTemplate is a prepared template. $id is set to a fresh template when
|
|
|
|
// starting to process a row.
|
|
|
|
$id = array();
|
|
|
|
$idTemplate = array();
|
|
|
|
|
|
|
|
// Holds the resulting hydrated data structure
|
2008-05-18 00:04:56 +04:00
|
|
|
if ($parserResult->isMixedQuery() || $hydrationMode == Doctrine::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) {
|
|
|
|
// disable lazy-loading of related elements during hydration
|
2008-06-15 19:56:28 +04:00
|
|
|
$component['metadata']->setAttribute('loadReferences', false);
|
2008-07-11 14:48:04 +04:00
|
|
|
$entityName = $component['metadata']->getClassName();
|
2008-05-06 17:41:22 +04:00
|
|
|
$identifierMap[$dqlAlias] = array();
|
|
|
|
$resultPointers[$dqlAlias] = array();
|
|
|
|
$idTemplate[$dqlAlias] = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$cache = array();
|
2008-05-18 20:21:55 +04:00
|
|
|
// Evaluate HYDRATE_SINGLE_SCALAR
|
|
|
|
if ($hydrationMode == Doctrine::HYDRATE_SINGLE_SCALAR) {
|
|
|
|
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
if (count($result) > 1 || count($result[0]) > 1) {
|
|
|
|
throw Doctrine_Hydrator_Exception::nonUniqueResult();
|
|
|
|
}
|
|
|
|
return array_shift($this->_gatherScalarRowData($result[0], $cache));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process result set
|
2008-05-18 00:04:56 +04:00
|
|
|
while ($data = $stmt->fetch(Doctrine::FETCH_ASSOC)) {
|
2008-05-18 20:21:55 +04:00
|
|
|
// Evaluate HYDRATE_SCALAR
|
2008-05-18 00:04:56 +04:00
|
|
|
if ($hydrationMode == Doctrine::HYDRATE_SCALAR) {
|
|
|
|
$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-11 14:48:04 +04:00
|
|
|
// 2) Hydrate the data of the root entity from the current row
|
|
|
|
//$class = $this->_queryComponents[$rootAlias]['metadata'];
|
|
|
|
//$entityName = $class->getComponentName();
|
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
|
|
|
|
);
|
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);
|
|
|
|
} 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-07-11 14:48:04 +04:00
|
|
|
$this->_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
|
|
|
|
// 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'];
|
|
|
|
$relationAlias = $relation->getAlias();
|
|
|
|
$path = $parent . '.' . $dqlAlias;
|
|
|
|
|
2008-05-08 18:17:35 +04:00
|
|
|
// pick the right element that 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
|
|
|
|
2008-05-06 17:41:22 +04:00
|
|
|
// check the type of the relation (many or single-valued)
|
|
|
|
if ( ! $relation->isOneToOne()) {
|
|
|
|
// x-many relation
|
|
|
|
$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;
|
|
|
|
$indexIsValid = $index !== false ? isset($baseElement[$relationAlias][$index]) : false;
|
|
|
|
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-05-08 18:17:35 +04:00
|
|
|
} else if ( ! isset($baseElement[$relationAlias])) {
|
2008-05-14 01:20:34 +04:00
|
|
|
$driver->setRelatedElement($baseElement, $relationAlias,
|
2008-07-11 14:48:04 +04:00
|
|
|
$driver->getElementCollection($entityName));
|
2008-05-06 17:41:22 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// x-1 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-05-14 01:20:34 +04:00
|
|
|
if (($coll =& $driver->getReferenceValue($baseElement, $relationAlias)) !== null) {
|
2008-07-11 14:48:04 +04:00
|
|
|
$this->_updateResultPointer($resultPointers, $coll, $index, $dqlAlias, $oneToOne);
|
2008-05-08 18:17:35 +04:00
|
|
|
}
|
2008-05-06 17:41:22 +04:00
|
|
|
}
|
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
// append scalar values to mixed result sets
|
2008-05-06 17:41:22 +04:00
|
|
|
if (isset($scalars)) {
|
|
|
|
$rowNumber = count($result) - 1;
|
|
|
|
foreach ($scalars as $name => $value) {
|
|
|
|
$result[$rowNumber][$name] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->closeCursor();
|
|
|
|
$driver->flush();
|
|
|
|
|
|
|
|
// re-enable lazy loading
|
|
|
|
foreach ($this->_queryComponents as $dqlAlias => $data) {
|
2008-06-15 19:56:28 +04:00
|
|
|
$data['metadata']->setAttribute('loadReferences', true);
|
2008-05-06 17:41:22 +04:00
|
|
|
}
|
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
$e = microtime(true);
|
|
|
|
echo 'Hydration took: ' . ($e - $s) . ' for '.count($result).' records' . PHP_EOL;
|
2008-05-06 17:41:22 +04:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-07-11 14:48:04 +04:00
|
|
|
* Updates the result pointer for an Entity. The result pointers point to the
|
|
|
|
* last seen instance of each Entity. This is used for graph construction.
|
2008-05-06 17:41:22 +04:00
|
|
|
*
|
2008-07-11 14:48:04 +04:00
|
|
|
* @param array $resultPointers The result pointers.
|
|
|
|
* @param array|Collection $coll The element.
|
2008-05-06 17:41:22 +04:00
|
|
|
* @param boolean|integer $index Index of the element in the collection.
|
|
|
|
* @param string $dqlAlias
|
|
|
|
* @param boolean $oneToOne Whether it is a single-valued association or not.
|
|
|
|
*/
|
2008-07-11 14:48:04 +04:00
|
|
|
protected function _updateResultPointer(&$resultPointers, &$coll, $index, $dqlAlias, $oneToOne)
|
2008-05-06 17:41:22 +04:00
|
|
|
{
|
2008-07-10 21:17:58 +04:00
|
|
|
if ($coll === $this->_nullObject || $coll === null) {
|
|
|
|
unset($resultPointers[$dqlAlias]); // Ticket #1228
|
2008-07-11 14:48:04 +04:00
|
|
|
return;
|
2008-05-06 17:41:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($index !== false) {
|
|
|
|
$resultPointers[$dqlAlias] =& $coll[$index];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($coll) && $coll) {
|
|
|
|
if ($oneToOne) {
|
|
|
|
$resultPointers[$dqlAlias] =& $coll;
|
|
|
|
} else {
|
|
|
|
end($coll);
|
|
|
|
$resultPointers[$dqlAlias] =& $coll[key($coll)];
|
|
|
|
}
|
2008-05-14 01:20:34 +04:00
|
|
|
} else if ($coll instanceof Doctrine_Entity) {
|
2008-05-06 17:41:22 +04:00
|
|
|
$resultPointers[$dqlAlias] = $coll;
|
|
|
|
} else if (count($coll) > 0) {
|
|
|
|
$resultPointers[$dqlAlias] = $coll->getLast();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-05-18 00:04:56 +04:00
|
|
|
* Processes a row of the result set.
|
|
|
|
* Used for identity hydration (HYDRATE_IDENTITY_OBJECT and HYDRATE_IDENTITY_ARRAY).
|
|
|
|
* Puts the elements of a result row into a new array, grouped by the class
|
2008-05-06 17:41:22 +04:00
|
|
|
* they belong to. The column names in the result set are mapped to their
|
2008-05-18 00:04:56 +04:00
|
|
|
* field names during this procedure as well as any necessary conversions on
|
|
|
|
* the values applied.
|
2008-05-06 17:41:22 +04:00
|
|
|
*
|
|
|
|
* @return array An array with all the fields (name => value) of the data row,
|
|
|
|
* grouped by their component (alias).
|
2008-05-18 20:21:55 +04:00
|
|
|
* @todo Significant code duplication with _gatherScalarRowData(). Good refactoring
|
|
|
|
* possible without sacrificing performance?
|
2008-05-06 17:41:22 +04:00
|
|
|
*/
|
|
|
|
protected function _gatherRowData(&$data, &$cache, &$id, &$nonemptyComponents)
|
|
|
|
{
|
|
|
|
$rowData = array();
|
|
|
|
|
|
|
|
foreach ($data as $key => $value) {
|
|
|
|
// Parse each column name only once. Cache the results.
|
|
|
|
if ( ! isset($cache[$key])) {
|
2008-05-18 00:04:56 +04:00
|
|
|
if ($this->_isIgnoredName($key)) continue;
|
2008-05-17 16:22:24 +04:00
|
|
|
|
2008-07-11 14:48:04 +04:00
|
|
|
// Cache general information like the column name <-> field name mapping
|
|
|
|
$e = explode(Doctrine_Query_Production::SQLALIAS_SEPARATOR, $key);
|
|
|
|
$columnName = array_pop($e);
|
|
|
|
$cache[$key]['dqlAlias'] = $this->_tableAliases[
|
|
|
|
implode(Doctrine_Query_Production::SQLALIAS_SEPARATOR, $e)
|
|
|
|
];
|
2008-06-15 19:56:28 +04:00
|
|
|
$classMetadata = $this->_queryComponents[$cache[$key]['dqlAlias']]['metadata'];
|
2008-05-06 17:41:22 +04:00
|
|
|
// check whether it's an aggregate value or a regular field
|
|
|
|
if (isset($this->_queryComponents[$cache[$key]['dqlAlias']]['agg'][$columnName])) {
|
|
|
|
$fieldName = $this->_queryComponents[$cache[$key]['dqlAlias']]['agg'][$columnName];
|
|
|
|
$cache[$key]['isScalar'] = true;
|
|
|
|
} else {
|
2008-05-25 01:29:01 +04:00
|
|
|
$fieldName = $classMetadata->lookupFieldName($columnName);
|
2008-05-06 17:41:22 +04:00
|
|
|
$cache[$key]['isScalar'] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$cache[$key]['fieldName'] = $fieldName;
|
|
|
|
|
|
|
|
// cache identifier information
|
|
|
|
if ($classMetadata->isIdentifier($fieldName)) {
|
|
|
|
$cache[$key]['isIdentifier'] = true;
|
|
|
|
} else {
|
|
|
|
$cache[$key]['isIdentifier'] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cache type information
|
|
|
|
$type = $classMetadata->getTypeOfColumn($columnName);
|
|
|
|
if ($type == 'integer' || $type == 'string') {
|
|
|
|
$cache[$key]['isSimpleType'] = true;
|
|
|
|
} else {
|
|
|
|
$cache[$key]['type'] = $type;
|
|
|
|
$cache[$key]['isSimpleType'] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-15 19:56:28 +04:00
|
|
|
$class = $this->_queryComponents[$cache[$key]['dqlAlias']]['metadata'];
|
2008-05-06 17:41:22 +04:00
|
|
|
$dqlAlias = $cache[$key]['dqlAlias'];
|
|
|
|
$fieldName = $cache[$key]['fieldName'];
|
|
|
|
|
|
|
|
if ($cache[$key]['isScalar']) {
|
|
|
|
$rowData['scalars'][$fieldName] = $value;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($cache[$key]['isIdentifier']) {
|
|
|
|
$id[$dqlAlias] .= '|' . $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($cache[$key]['isSimpleType']) {
|
|
|
|
$rowData[$dqlAlias][$fieldName] = $value;
|
|
|
|
} else {
|
2008-05-25 01:32:21 +04:00
|
|
|
$rowData[$dqlAlias][$fieldName] = $this->prepareValue(
|
|
|
|
$class, $fieldName, $value, $cache[$key]['type']);
|
2008-05-06 17:41:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! isset($nonemptyComponents[$dqlAlias]) && $value !== null) {
|
|
|
|
$nonemptyComponents[$dqlAlias] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rowData;
|
|
|
|
}
|
|
|
|
|
2008-05-18 00:04:56 +04:00
|
|
|
/**
|
|
|
|
* Processes a row of the result set.
|
|
|
|
* Used for HYDRATE_SCALAR. This is a variant of _gatherRowData() that
|
|
|
|
* simply converts column names to field names and properly prepares the
|
|
|
|
* values. The resulting row has the same number of elements as before.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param array $cache
|
|
|
|
* @return array The processed row.
|
2008-05-18 20:21:55 +04:00
|
|
|
* @todo Significant code duplication with _gatherRowData(). Good refactoring
|
|
|
|
* possible without sacrificing performance?
|
2008-05-18 00:04:56 +04:00
|
|
|
*/
|
|
|
|
private function _gatherScalarRowData(&$data, &$cache)
|
|
|
|
{
|
|
|
|
$rowData = array();
|
|
|
|
|
|
|
|
foreach ($data as $key => $value) {
|
|
|
|
// Parse each column name only once. Cache the results.
|
|
|
|
if ( ! isset($cache[$key])) {
|
|
|
|
if ($this->_isIgnoredName($key)) continue;
|
|
|
|
|
|
|
|
// cache general information like the column name <-> field name mapping
|
2008-07-11 14:48:04 +04:00
|
|
|
$e = explode(Doctrine_Query_Production::SQLALIAS_SEPARATOR, $key);
|
|
|
|
$columnName = array_pop($e);
|
|
|
|
$cache[$key]['dqlAlias'] = $this->_tableAliases[
|
|
|
|
implode(Doctrine_Query_Production::SQLALIAS_SEPARATOR, $e)
|
|
|
|
];
|
2008-06-15 19:56:28 +04:00
|
|
|
$classMetadata = $this->_queryComponents[$cache[$key]['dqlAlias']]['metadata'];
|
2008-05-18 00:04:56 +04:00
|
|
|
// check whether it's an aggregate value or a regular field
|
|
|
|
if (isset($this->_queryComponents[$cache[$key]['dqlAlias']]['agg'][$columnName])) {
|
|
|
|
$fieldName = $this->_queryComponents[$cache[$key]['dqlAlias']]['agg'][$columnName];
|
|
|
|
$cache[$key]['isScalar'] = true;
|
|
|
|
} else {
|
2008-05-25 01:29:01 +04:00
|
|
|
$fieldName = $classMetadata->lookupFieldName($columnName);
|
2008-05-18 00:04:56 +04:00
|
|
|
$cache[$key]['isScalar'] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$cache[$key]['fieldName'] = $fieldName;
|
|
|
|
|
|
|
|
// cache type information
|
|
|
|
$type = $classMetadata->getTypeOfColumn($columnName);
|
|
|
|
if ($type == 'integer' || $type == 'string') {
|
|
|
|
$cache[$key]['isSimpleType'] = true;
|
|
|
|
} else {
|
|
|
|
$cache[$key]['type'] = $type;
|
|
|
|
$cache[$key]['isSimpleType'] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-15 19:56:28 +04:00
|
|
|
$class = $this->_queryComponents[$cache[$key]['dqlAlias']]['metadata'];
|
2008-05-18 00:04:56 +04:00
|
|
|
$dqlAlias = $cache[$key]['dqlAlias'];
|
|
|
|
$fieldName = $cache[$key]['fieldName'];
|
|
|
|
|
|
|
|
if ($cache[$key]['isSimpleType'] || $cache[$key]['isScalar']) {
|
|
|
|
$rowData[$dqlAlias . '_' . $fieldName] = $value;
|
|
|
|
} else {
|
2008-05-25 01:29:01 +04:00
|
|
|
$rowData[$dqlAlias . '_' . $fieldName] = $this->prepareValue(
|
|
|
|
$class, $fieldName, $value, $cache[$key]['type']);
|
2008-05-18 00:04:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rowData;
|
|
|
|
}
|
|
|
|
|
2008-05-06 17:41:22 +04:00
|
|
|
/**
|
|
|
|
* Gets the custom field used for indexing for the specified component alias.
|
|
|
|
*
|
|
|
|
* @return string The field name of the field used for indexing or NULL
|
|
|
|
* if the component does not use any custom field indices.
|
|
|
|
*/
|
2008-05-18 00:04:56 +04:00
|
|
|
private function _getCustomIndexField($alias)
|
2008-05-06 17:41:22 +04:00
|
|
|
{
|
|
|
|
return isset($this->_queryComponents[$alias]['map']) ? $this->_queryComponents[$alias]['map'] : null;
|
|
|
|
}
|
|
|
|
|
2008-05-18 00:04:56 +04:00
|
|
|
/**
|
|
|
|
* Checks whether a name is ignored. Used during result set parsing to skip
|
|
|
|
* certain elements in the result set that do not have any meaning for the result.
|
|
|
|
* (I.e. ORACLE limit/offset emulation adds doctrine_rownum to the result set).
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
private function _isIgnoredName($name)
|
|
|
|
{
|
|
|
|
return $name == 'doctrine_rownum';
|
|
|
|
}
|
2008-05-06 17:41:22 +04:00
|
|
|
|
2008-05-25 01:29:01 +04:00
|
|
|
/**
|
|
|
|
* prepareValue
|
|
|
|
* this method performs special data preparation depending on
|
|
|
|
* the type of the given column
|
|
|
|
*
|
|
|
|
* 1. It unserializes array and object typed columns
|
|
|
|
* 2. Uncompresses gzip typed columns
|
|
|
|
* 3. Gets the appropriate enum values for enum typed columns
|
|
|
|
* 4. Initializes special null object pointer for null values (for fast column existence checking purposes)
|
|
|
|
*
|
|
|
|
* example:
|
|
|
|
* <code type='php'>
|
|
|
|
* $field = 'name';
|
|
|
|
* $value = null;
|
|
|
|
* $table->prepareValue($field, $value); // Doctrine_Null
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @param string $field the name of the field
|
|
|
|
* @param string $value field value
|
|
|
|
* @param string $typeHint A hint on the type of the value. If provided, the type lookup
|
|
|
|
* for the field can be skipped. Used i.e. during hydration to
|
|
|
|
* improve performance on large and/or complex results.
|
|
|
|
* @return mixed prepared value
|
|
|
|
*/
|
|
|
|
public function prepareValue(Doctrine_ClassMetadata $class, $fieldName, $value, $typeHint = null)
|
|
|
|
{
|
|
|
|
if ($value === $this->_nullObject) {
|
|
|
|
return $this->_nullObject;
|
|
|
|
} else if ($value === null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
$type = is_null($typeHint) ? $class->getTypeOf($fieldName) : $typeHint;
|
|
|
|
switch ($type) {
|
|
|
|
case 'integer':
|
2008-07-21 00:13:24 +04:00
|
|
|
case 'string':
|
2008-05-25 01:29:01 +04:00
|
|
|
case 'enum':
|
|
|
|
case 'boolean':
|
2008-07-21 00:13:24 +04:00
|
|
|
// don't do any conversions on primitive types
|
2008-05-25 01:29:01 +04:00
|
|
|
break;
|
2008-07-21 00:13:24 +04:00
|
|
|
//case 'enum':
|
|
|
|
// return $class->enumValue($fieldName, $value);
|
|
|
|
//break;
|
|
|
|
//case 'boolean':
|
|
|
|
// return (boolean) $value;
|
|
|
|
//break;
|
2008-05-25 01:29:01 +04:00
|
|
|
case 'array':
|
|
|
|
case 'object':
|
|
|
|
if (is_string($value)) {
|
|
|
|
$value = unserialize($value);
|
|
|
|
if ($value === false) {
|
|
|
|
throw new Doctrine_Hydrator_Exception('Unserialization of ' . $fieldName . ' failed.');
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'gzip':
|
|
|
|
$value = gzuncompress($value);
|
|
|
|
if ($value === false) {
|
|
|
|
throw new Doctrine_Hydrator_Exception('Uncompressing of ' . $fieldName . ' failed.');
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-05-18 00:04:56 +04:00
|
|
|
/** Needed only temporarily until the new parser is ready */
|
2008-05-06 17:41:22 +04:00
|
|
|
private $_isResultMixed = false;
|
|
|
|
public function setResultMixed($bool)
|
|
|
|
{
|
|
|
|
$this->_isResultMixed = $bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|