2009-01-14 00:56:43 +03:00
|
|
|
<?php
|
2009-04-03 15:06:58 +04:00
|
|
|
/*
|
|
|
|
* $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.doctrine-project.org>.
|
2009-01-14 00:56:43 +03:00
|
|
|
*/
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\ORM\Internal\Hydration;
|
|
|
|
|
2009-05-29 19:38:46 +04:00
|
|
|
use Doctrine\DBAL\Connection;
|
2009-01-22 22:38:10 +03:00
|
|
|
|
2009-01-14 00:56:43 +03:00
|
|
|
/**
|
2009-07-20 14:52:07 +04:00
|
|
|
* The ArrayHydrator produces a nested array "graph" that is often (not always)
|
|
|
|
* interchangeable with the corresponding object graph for read-only access.
|
2009-01-14 00:56:43 +03:00
|
|
|
*
|
2009-07-20 14:52:07 +04:00
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
* @since 1.0
|
2009-01-14 00:56:43 +03:00
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
class ArrayHydrator extends AbstractHydrator
|
2009-01-14 00:56:43 +03:00
|
|
|
{
|
2009-08-11 14:51:38 +04:00
|
|
|
private $_ce = array();
|
2009-04-09 22:12:48 +04:00
|
|
|
private $_rootAliases = array();
|
2009-01-14 00:56:43 +03:00
|
|
|
private $_isSimpleQuery = false;
|
|
|
|
private $_identifierMap = array();
|
|
|
|
private $_resultPointers = array();
|
|
|
|
private $_idTemplate = array();
|
|
|
|
private $_resultCounter = 0;
|
|
|
|
|
|
|
|
/** @override */
|
2009-04-12 23:02:12 +04:00
|
|
|
protected function _prepare()
|
2009-01-14 00:56:43 +03:00
|
|
|
{
|
2009-07-30 19:16:02 +04:00
|
|
|
$this->_isSimpleQuery = count($this->_rsm->aliasMap) <= 1;
|
2009-01-14 00:56:43 +03:00
|
|
|
$this->_identifierMap = array();
|
|
|
|
$this->_resultPointers = array();
|
|
|
|
$this->_idTemplate = array();
|
|
|
|
$this->_resultCounter = 0;
|
2009-07-30 19:16:02 +04:00
|
|
|
foreach ($this->_rsm->aliasMap as $dqlAlias => $className) {
|
2009-01-14 00:56:43 +03:00
|
|
|
$this->_identifierMap[$dqlAlias] = array();
|
|
|
|
$this->_resultPointers[$dqlAlias] = array();
|
|
|
|
$this->_idTemplate[$dqlAlias] = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @override */
|
2009-01-15 16:30:44 +03:00
|
|
|
protected function _hydrateAll()
|
2009-01-14 00:56:43 +03:00
|
|
|
{
|
|
|
|
$result = array();
|
|
|
|
$cache = array();
|
2009-05-29 19:38:46 +04:00
|
|
|
while ($data = $this->_stmt->fetch(Connection::FETCH_ASSOC)) {
|
2009-01-14 00:56:43 +03:00
|
|
|
$this->_hydrateRow($data, $cache, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @override */
|
2009-08-03 17:25:56 +04:00
|
|
|
protected function _hydrateRow(array &$data, array &$cache, array &$result)
|
2009-01-14 00:56:43 +03:00
|
|
|
{
|
|
|
|
// 1) Initialize
|
|
|
|
$id = $this->_idTemplate; // initialize the id-memory
|
|
|
|
$nonemptyComponents = array();
|
2009-01-15 16:30:44 +03:00
|
|
|
$rowData = $this->_gatherRowData($data, $cache, $id, $nonemptyComponents);
|
2009-01-14 00:56:43 +03:00
|
|
|
|
|
|
|
// Extract scalar values. They're appended at the end.
|
|
|
|
if (isset($rowData['scalars'])) {
|
|
|
|
$scalars = $rowData['scalars'];
|
|
|
|
unset($rowData['scalars']);
|
|
|
|
}
|
|
|
|
|
2009-12-07 15:47:23 +03:00
|
|
|
// 2) Now hydrate the data found in the current row.
|
2009-01-14 00:56:43 +03:00
|
|
|
foreach ($rowData as $dqlAlias => $data) {
|
|
|
|
$index = false;
|
|
|
|
|
2009-05-14 22:34:12 +04:00
|
|
|
if (isset($this->_rsm->parentAliasMap[$dqlAlias])) {
|
2009-05-14 18:57:08 +04:00
|
|
|
// It's a joined result
|
2009-04-09 22:12:48 +04:00
|
|
|
|
2009-05-14 22:34:12 +04:00
|
|
|
$parent = $this->_rsm->parentAliasMap[$dqlAlias];
|
2009-04-09 22:12:48 +04:00
|
|
|
$path = $parent . '.' . $dqlAlias;
|
|
|
|
|
|
|
|
// Get a reference to the right element in the result tree.
|
|
|
|
// This element will get the associated element attached.
|
2009-05-14 22:34:12 +04:00
|
|
|
if ($this->_rsm->isMixed && isset($this->_rootAliases[$parent])) {
|
2009-05-21 23:18:14 +04:00
|
|
|
$first = reset($this->_resultPointers);
|
2009-04-09 22:12:48 +04:00
|
|
|
// TODO: Exception if $key === null ?
|
2009-05-21 23:18:14 +04:00
|
|
|
$baseElement =& $this->_resultPointers[$parent][key($first)];
|
2009-04-09 22:12:48 +04:00
|
|
|
} else if (isset($this->_resultPointers[$parent])) {
|
|
|
|
$baseElement =& $this->_resultPointers[$parent];
|
|
|
|
} else {
|
|
|
|
unset($this->_resultPointers[$dqlAlias]); // Ticket #1228
|
|
|
|
continue;
|
|
|
|
}
|
2009-07-30 19:16:02 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
$relationAlias = $this->_rsm->relationMap[$dqlAlias];
|
|
|
|
$relation = $this->_getClassMetadata($this->_rsm->aliasMap[$parent])->associationMappings[$relationAlias];
|
2009-04-09 22:12:48 +04:00
|
|
|
|
|
|
|
// Check the type of the relation (many or single-valued)
|
|
|
|
if ( ! $relation->isOneToOne()) {
|
|
|
|
$oneToOne = false;
|
|
|
|
if (isset($nonemptyComponents[$dqlAlias])) {
|
|
|
|
if ( ! isset($baseElement[$relationAlias])) {
|
|
|
|
$baseElement[$relationAlias] = array();
|
|
|
|
}
|
2009-07-30 19:16:02 +04:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
$indexExists = isset($this->_identifierMap[$path][$id[$parent]][$id[$dqlAlias]]);
|
|
|
|
$index = $indexExists ? $this->_identifierMap[$path][$id[$parent]][$id[$dqlAlias]] : false;
|
|
|
|
$indexIsValid = $index !== false ? isset($baseElement[$relationAlias][$index]) : false;
|
2009-07-30 19:16:02 +04:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
if ( ! $indexExists || ! $indexIsValid) {
|
|
|
|
$element = $data;
|
2009-07-20 14:52:07 +04:00
|
|
|
if (isset($this->_rsm->indexByMap[$dqlAlias])) {
|
|
|
|
$field = $this->_rsm->indexByMap[$dqlAlias];
|
2009-04-09 22:12:48 +04:00
|
|
|
$baseElement[$relationAlias][$element[$field]] = $element;
|
|
|
|
} else {
|
|
|
|
$baseElement[$relationAlias][] = $element;
|
|
|
|
}
|
|
|
|
end($baseElement[$relationAlias]);
|
|
|
|
$this->_identifierMap[$path][$id[$parent]][$id[$dqlAlias]] =
|
|
|
|
key($baseElement[$relationAlias]);
|
|
|
|
}
|
|
|
|
} else if ( ! isset($baseElement[$relationAlias])) {
|
2009-01-14 00:56:43 +03:00
|
|
|
$baseElement[$relationAlias] = array();
|
|
|
|
}
|
2009-04-09 22:12:48 +04:00
|
|
|
} else {
|
|
|
|
$oneToOne = true;
|
|
|
|
if ( ! isset($nonemptyComponents[$dqlAlias]) && ! isset($baseElement[$relationAlias])) {
|
|
|
|
$baseElement[$relationAlias] = null;
|
|
|
|
} else if ( ! isset($baseElement[$relationAlias])) {
|
|
|
|
$baseElement[$relationAlias] = $data;
|
2009-01-14 00:56:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
$coll =& $baseElement[$relationAlias];
|
|
|
|
|
|
|
|
if ($coll !== null) {
|
|
|
|
$this->updateResultPointer($coll, $index, $dqlAlias, $oneToOne);
|
|
|
|
}
|
2009-01-14 00:56:43 +03:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
} else {
|
2009-07-20 14:52:07 +04:00
|
|
|
// It's a root result element
|
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
$this->_rootAliases[$dqlAlias] = true; // Mark as root
|
2009-07-20 14:52:07 +04:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
// Check for an existing element
|
|
|
|
if ($this->_isSimpleQuery || ! isset($this->_identifierMap[$dqlAlias][$id[$dqlAlias]])) {
|
|
|
|
$element = $rowData[$dqlAlias];
|
2009-07-20 14:52:07 +04:00
|
|
|
if (isset($this->_rsm->indexByMap[$dqlAlias])) {
|
|
|
|
$field = $this->_rsm->indexByMap[$dqlAlias];
|
2009-05-14 22:34:12 +04:00
|
|
|
if ($this->_rsm->isMixed) {
|
2009-04-09 22:12:48 +04:00
|
|
|
$result[] = array($element[$field] => $element);
|
|
|
|
++$this->_resultCounter;
|
|
|
|
} else {
|
|
|
|
$result[$element[$field]] = $element;
|
|
|
|
}
|
|
|
|
} else {
|
2009-05-14 22:34:12 +04:00
|
|
|
if ($this->_rsm->isMixed) {
|
2009-04-09 22:12:48 +04:00
|
|
|
$result[] = array($element);
|
|
|
|
++$this->_resultCounter;
|
|
|
|
} else {
|
|
|
|
$result[] = $element;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end($result);
|
|
|
|
$this->_identifierMap[$dqlAlias][$id[$dqlAlias]] = key($result);
|
|
|
|
} else {
|
|
|
|
$index = $this->_identifierMap[$dqlAlias][$id[$dqlAlias]];
|
|
|
|
}
|
|
|
|
$this->updateResultPointer($result, $index, $dqlAlias, false);
|
2009-01-14 00:56:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append scalar values to mixed result sets
|
|
|
|
if (isset($scalars)) {
|
|
|
|
foreach ($scalars as $name => $value) {
|
|
|
|
$result[$this->_resultCounter - 1][$name] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the result pointer for an Entity. The result pointers point to the
|
|
|
|
* last seen instance of each Entity type. This is used for graph construction.
|
|
|
|
*
|
|
|
|
* @param array $coll The element.
|
|
|
|
* @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.
|
|
|
|
*/
|
2009-08-03 17:25:56 +04:00
|
|
|
private function updateResultPointer(array &$coll, $index, $dqlAlias, $oneToOne)
|
2009-01-14 00:56:43 +03:00
|
|
|
{
|
|
|
|
if ($coll === null) {
|
|
|
|
unset($this->_resultPointers[$dqlAlias]); // Ticket #1228
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($index !== false) {
|
|
|
|
$this->_resultPointers[$dqlAlias] =& $coll[$index];
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if ($coll) {
|
|
|
|
if ($oneToOne) {
|
|
|
|
$this->_resultPointers[$dqlAlias] =& $coll;
|
|
|
|
} else {
|
|
|
|
end($coll);
|
|
|
|
$this->_resultPointers[$dqlAlias] =& $coll[key($coll)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-11 14:51:38 +04:00
|
|
|
|
|
|
|
private function _getClassMetadata($className)
|
|
|
|
{
|
|
|
|
if ( ! isset($this->_ce[$className])) {
|
|
|
|
$this->_ce[$className] = $this->_em->getClassMetadata($className);
|
|
|
|
}
|
|
|
|
return $this->_ce[$className];
|
|
|
|
}
|
2009-02-20 08:46:20 +03:00
|
|
|
}
|