1
0
mirror of synced 2024-12-15 23:56:02 +03:00
doctrine2/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php

630 lines
25 KiB
PHP
Raw Normal View History

<?php
/*
* 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
2012-05-26 16:37:00 +04:00
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ORM\Internal\Hydration;
2013-04-08 13:27:22 +04:00
use Doctrine\ORM\UnitOfWork;
2012-08-12 04:38:02 +04:00
use PDO;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\PersistentCollection;
use Doctrine\ORM\Query;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Proxy\Proxy;
/**
* The ObjectHydrator constructs an object graph out of an SQL result set.
*
* @since 2.0
* @author Roman Borschel <roman@code-factory.org>
* @author Guilherme Blanco <guilhermeblanoc@hotmail.com>
2012-08-12 04:38:02 +04:00
* @author Fabio B. Silva <fabio.bat.silva@gmail.com>
*
2009-10-09 18:27:35 +04:00
* @internal Highly performance-sensitive code.
*/
class ObjectHydrator extends AbstractHydrator
{
2012-12-02 23:44:56 +04:00
/**
* Local ClassMetadata cache to avoid going to the EntityManager all the time.
* This local cache is maintained between hydration runs and not cleared.
2012-12-02 23:44:56 +04:00
*
* @var array
*/
2012-08-12 04:38:02 +04:00
private $ce = array();
/* The following parts are reinitialized on every hydration run. */
2012-08-12 04:38:02 +04:00
/**
* @var array
*/
private $identifierMap;
/**
* @var array
*/
private $resultPointers;
/**
* @var array
*/
private $idTemplate;
2012-08-12 04:38:02 +04:00
/**
* @var integer
*/
private $resultCounter;
2012-08-12 04:38:02 +04:00
/**
* @var array
*/
private $rootAliases = array();
/**
* @var array
*/
private $initializedCollections = array();
/**
* @var array
*/
private $existingCollections = array();
/**
2012-08-21 05:43:16 +04:00
* {@inheritdoc}
2012-08-12 04:38:02 +04:00
*/
protected function prepare()
{
2012-08-12 04:38:02 +04:00
$this->identifierMap =
$this->resultPointers =
$this->idTemplate = array();
2012-08-12 04:38:02 +04:00
$this->resultCounter = 0;
2013-04-08 13:27:22 +04:00
if ( ! isset($this->_hints[UnitOfWork::HINT_DEFEREAGERLOAD])) {
$this->_hints[UnitOfWork::HINT_DEFEREAGERLOAD] = true;
}
foreach ($this->_rsm->aliasMap as $dqlAlias => $className) {
2012-08-12 04:38:02 +04:00
$this->identifierMap[$dqlAlias] = array();
$this->idTemplate[$dqlAlias] = '';
2012-08-12 04:38:02 +04:00
if ( ! isset($this->ce[$className])) {
$this->ce[$className] = $this->_em->getClassMetadata($className);
}
// Remember which associations are "fetch joined", so that we know where to inject
// collection stubs or proxies and where not.
if ( ! isset($this->_rsm->relationMap[$dqlAlias])) {
continue;
}
if ( ! isset($this->_rsm->aliasMap[$this->_rsm->parentAliasMap[$dqlAlias]])) {
throw HydrationException::parentObjectOfRelationNotFound($dqlAlias, $this->_rsm->parentAliasMap[$dqlAlias]);
}
$sourceClassName = $this->_rsm->aliasMap[$this->_rsm->parentAliasMap[$dqlAlias]];
2012-08-12 04:52:03 +04:00
$sourceClass = $this->getClassMetadata($sourceClassName);
$assoc = $sourceClass->associationMappings[$this->_rsm->relationMap[$dqlAlias]];
$this->_hints['fetched'][$this->_rsm->parentAliasMap[$dqlAlias]][$assoc['fieldName']] = true;
if ($assoc['type'] === ClassMetadata::MANY_TO_MANY) {
continue;
}
// Mark any non-collection opposite sides as fetched, too.
if ($assoc['mappedBy']) {
$this->_hints['fetched'][$dqlAlias][$assoc['mappedBy']] = true;
continue;
}
// handle fetch-joined owning side bi-directional one-to-one associations
if ($assoc['inversedBy']) {
2012-08-12 04:38:02 +04:00
$class = $this->ce[$className];
$inverseAssoc = $class->associationMappings[$assoc['inversedBy']];
if ( ! ($inverseAssoc['type'] & ClassMetadata::TO_ONE)) {
continue;
2010-01-30 00:24:29 +03:00
}
$this->_hints['fetched'][$dqlAlias][$inverseAssoc['fieldName']] = true;
}
}
}
/**
* {@inheritdoc}
*/
protected function cleanup()
{
2013-04-08 13:27:22 +04:00
$eagerLoad = (isset($this->_hints[UnitOfWork::HINT_DEFEREAGERLOAD])) && $this->_hints[UnitOfWork::HINT_DEFEREAGERLOAD] == true;
parent::cleanup();
2012-08-12 04:38:02 +04:00
$this->identifierMap =
$this->initializedCollections =
$this->existingCollections =
$this->resultPointers = array();
if ($eagerLoad) {
$this->_em->getUnitOfWork()->triggerEagerLoads();
}
}
/**
* {@inheritdoc}
*/
protected function hydrateAllData()
{
$result = array();
$cache = array();
while ($row = $this->_stmt->fetch(PDO::FETCH_ASSOC)) {
$this->hydrateRowData($row, $cache, $result);
}
2009-10-09 18:27:35 +04:00
// Take snapshots from all newly initialized collections
2012-08-12 04:38:02 +04:00
foreach ($this->initializedCollections as $coll) {
$coll->takeSnapshot();
}
return $result;
}
/**
* Initializes a related collection.
*
2012-07-12 02:44:07 +04:00
* @param object $entity The entity to which the collection belongs.
* @param ClassMetadata $class
2012-07-12 02:44:07 +04:00
* @param string $fieldName The name of the field on the entity that holds the collection.
* @param string $parentDqlAlias Alias of the parent fetch joining this collection.
2012-12-02 23:44:56 +04:00
*
* @return \Doctrine\ORM\PersistentCollection
*/
2012-08-12 04:52:03 +04:00
private function initRelatedCollection($entity, $class, $fieldName, $parentDqlAlias)
{
$oid = spl_object_hash($entity);
2009-12-07 23:35:44 +03:00
$relation = $class->associationMappings[$fieldName];
$value = $class->reflFields[$fieldName]->getValue($entity);
2009-09-06 19:25:23 +04:00
if ($value === null) {
$value = new ArrayCollection;
}
if ( ! $value instanceof PersistentCollection) {
2009-09-06 19:25:23 +04:00
$value = new PersistentCollection(
2012-08-12 04:38:02 +04:00
$this->_em, $this->ce[$relation['targetEntity']], $value
2009-09-06 19:25:23 +04:00
);
$value->setOwner($entity, $relation);
2009-12-07 23:35:44 +03:00
$class->reflFields[$fieldName]->setValue($entity, $value);
$this->_uow->setOriginalEntityProperty($oid, $fieldName, $value);
2012-08-12 04:38:02 +04:00
$this->initializedCollections[$oid . $fieldName] = $value;
} else if (
isset($this->_hints[Query::HINT_REFRESH]) ||
isset($this->_hints['fetched'][$parentDqlAlias][$fieldName]) &&
! $value->isInitialized()
) {
// Is already PersistentCollection, but either REFRESH or FETCH-JOIN and UNINITIALIZED!
2009-09-06 19:25:23 +04:00
$value->setDirty(false);
$value->setInitialized(true);
$value->unwrap()->clear();
2012-08-12 04:38:02 +04:00
$this->initializedCollections[$oid . $fieldName] = $value;
2009-10-09 18:27:35 +04:00
} else {
// Is already PersistentCollection, and DON'T REFRESH or FETCH-JOIN!
2012-08-12 04:38:02 +04:00
$this->existingCollections[$oid . $fieldName] = $value;
2009-09-06 19:25:23 +04:00
}
2009-09-06 19:25:23 +04:00
return $value;
}
/**
* Gets an entity instance.
*
2012-07-12 02:44:07 +04:00
* @param array $data The instance data.
2011-12-30 21:40:19 +04:00
* @param string $dqlAlias The DQL alias of the entity's class.
2012-12-02 23:44:56 +04:00
*
* @return object The entity.
2012-12-02 23:44:56 +04:00
*
* @throws HydrationException
*/
2012-08-12 04:52:03 +04:00
private function getEntity(array $data, $dqlAlias)
{
$className = $this->_rsm->aliasMap[$dqlAlias];
if (isset($this->_rsm->discriminatorColumns[$dqlAlias])) {
2012-03-16 04:29:07 +04:00
2012-03-16 05:38:54 +04:00
if ( ! isset($this->_rsm->metaMappings[$this->_rsm->discriminatorColumns[$dqlAlias]])) {
2012-03-16 04:29:07 +04:00
throw HydrationException::missingDiscriminatorMetaMappingColumn($className, $this->_rsm->discriminatorColumns[$dqlAlias], $dqlAlias);
}
$discrColumn = $this->_rsm->metaMappings[$this->_rsm->discriminatorColumns[$dqlAlias]];
2012-03-16 05:38:54 +04:00
if ( ! isset($data[$discrColumn])) {
2012-03-16 04:29:07 +04:00
throw HydrationException::missingDiscriminatorColumn($className, $discrColumn, $dqlAlias);
}
if ($data[$discrColumn] === "") {
throw HydrationException::emptyDiscriminatorValue($dqlAlias);
}
2012-08-12 04:38:02 +04:00
$className = $this->ce[$className]->discriminatorMap[$data[$discrColumn]];
unset($data[$discrColumn]);
}
2012-08-12 04:38:02 +04:00
if (isset($this->_hints[Query::HINT_REFRESH_ENTITY]) && isset($this->rootAliases[$dqlAlias])) {
$this->registerManaged($this->ce[$className], $this->_hints[Query::HINT_REFRESH_ENTITY], $data);
}
$this->_hints['fetchAlias'] = $dqlAlias;
2011-12-20 01:56:19 +04:00
return $this->_uow->createEntity($className, $data, $this->_hints);
}
2012-07-12 02:44:07 +04:00
/**
* @param string $className
* @param array $data
2012-12-02 23:44:56 +04:00
*
2012-07-12 02:44:07 +04:00
* @return mixed
*/
2012-08-12 04:52:03 +04:00
private function getEntityFromIdentityMap($className, array $data)
2009-10-09 18:27:35 +04:00
{
// TODO: Abstract this code and UnitOfWork::createEntity() equivalent?
2012-08-12 04:38:02 +04:00
$class = $this->ce[$className];
/* @var $class ClassMetadata */
2009-10-09 18:27:35 +04:00
if ($class->isIdentifierComposite) {
$idHash = '';
foreach ($class->identifier as $fieldName) {
if (isset($class->associationMappings[$fieldName])) {
$idHash .= $data[$class->associationMappings[$fieldName]['joinColumns'][0]['name']] . ' ';
} else {
$idHash .= $data[$fieldName] . ' ';
}
2009-10-09 18:27:35 +04:00
}
return $this->_uow->tryGetByIdHash(rtrim($idHash), $class->rootEntityName);
} else if (isset($class->associationMappings[$class->identifier[0]])) {
return $this->_uow->tryGetByIdHash($data[$class->associationMappings[$class->identifier[0]]['joinColumns'][0]['name']], $class->rootEntityName);
2009-10-09 18:27:35 +04:00
} else {
return $this->_uow->tryGetByIdHash($data[$class->identifier[0]], $class->rootEntityName);
2009-10-09 18:27:35 +04:00
}
}
2009-07-28 20:36:24 +04:00
/**
* Gets a ClassMetadata instance from the local cache.
* If the instance is not yet in the local cache, it is loaded into the
* local cache.
*
2009-07-28 20:36:24 +04:00
* @param string $className The name of the class.
2012-12-02 23:44:56 +04:00
*
2009-07-28 20:36:24 +04:00
* @return ClassMetadata
*/
2012-08-12 04:52:03 +04:00
private function getClassMetadata($className)
2009-07-28 20:36:24 +04:00
{
2012-08-12 04:38:02 +04:00
if ( ! isset($this->ce[$className])) {
$this->ce[$className] = $this->_em->getClassMetadata($className);
2009-07-28 20:36:24 +04:00
}
2012-08-12 04:38:02 +04:00
return $this->ce[$className];
2009-07-28 20:36:24 +04:00
}
/**
2009-10-19 00:36:02 +04:00
* Hydrates a single row in an SQL result set.
*
2009-10-19 00:36:02 +04:00
* @internal
* First, the data of the row is split into chunks where each chunk contains data
* that belongs to a particular component/class. Afterwards, all these chunks
* are processed, one after the other. For each chunk of class data only one of the
* following code paths is executed:
*
2009-10-19 00:36:02 +04:00
* Path A: The data chunk belongs to a joined/associated object and the association
* is collection-valued.
* Path B: The data chunk belongs to a joined/associated object and the association
* is single-valued.
* Path C: The data chunk belongs to a root result element/object that appears in the topmost
* level of the hydrated result. A typical example are the objects of the type
* specified by the FROM clause in a DQL query.
*
2012-07-12 02:44:07 +04:00
* @param array $row The data of the row to process.
* @param array $cache The cache to use.
* @param array $result The result array to fill.
2012-12-02 23:44:56 +04:00
*
* @return void
*/
protected function hydrateRowData(array $row, array &$cache, array &$result)
{
// Initialize
2012-08-12 04:38:02 +04:00
$id = $this->idTemplate; // initialize the id-memory
$nonemptyComponents = array();
2009-10-19 00:36:02 +04:00
// Split the row data into chunks of class data.
$rowData = $this->gatherRowData($row, $cache, $id, $nonemptyComponents);
// Extract scalar values. They're appended at the end.
if (isset($rowData['scalars'])) {
$scalars = $rowData['scalars'];
unset($rowData['scalars']);
if (empty($rowData)) {
2012-08-12 04:38:02 +04:00
++$this->resultCounter;
}
}
// Extract "new" object constructor arguments. They're appended at the end.
if (isset($rowData['newObjects'])) {
$newObjects = $rowData['newObjects'];
unset($rowData['newObjects']);
if (empty($rowData)) {
2012-08-12 04:38:02 +04:00
++$this->resultCounter;
}
}
2009-10-19 00:36:02 +04:00
// Hydrate the data chunks
foreach ($rowData as $dqlAlias => $data) {
$entityName = $this->_rsm->aliasMap[$dqlAlias];
if (isset($this->_rsm->parentAliasMap[$dqlAlias])) {
// It's a joined result
2009-10-09 18:27:35 +04:00
$parentAlias = $this->_rsm->parentAliasMap[$dqlAlias];
// we need the $path to save into the identifier map which entities were already
// seen for this parent-child relationship
$path = $parentAlias . '.' . $dqlAlias;
// We have a RIGHT JOIN result here. Doctrine cannot hydrate RIGHT JOIN Object-Graphs
2012-03-16 05:38:54 +04:00
if ( ! isset($nonemptyComponents[$parentAlias])) {
// TODO: Add special case code where we hydrate the right join objects into identity map at least
continue;
}
2009-10-09 18:27:35 +04:00
// Get a reference to the parent object to which the joined element belongs.
2012-08-12 04:38:02 +04:00
if ($this->_rsm->isMixed && isset($this->rootAliases[$parentAlias])) {
$first = reset($this->resultPointers);
2011-11-15 07:10:27 +04:00
$parentObject = $first[key($first)];
2012-08-12 04:38:02 +04:00
} else if (isset($this->resultPointers[$parentAlias])) {
$parentObject = $this->resultPointers[$parentAlias];
} else {
2009-10-09 18:27:35 +04:00
// Parent object of relation not found, so skip it.
continue;
}
2012-08-12 21:40:26 +04:00
$parentClass = $this->ce[$this->_rsm->aliasMap[$parentAlias]];
$oid = spl_object_hash($parentObject);
$relationField = $this->_rsm->relationMap[$dqlAlias];
$relation = $parentClass->associationMappings[$relationField];
$reflField = $parentClass->reflFields[$relationField];
// Check the type of the relation (many or single-valued)
if ( ! ($relation['type'] & ClassMetadata::TO_ONE)) {
$reflFieldValue = $reflField->getValue($parentObject);
2009-10-19 00:36:02 +04:00
// PATH A: Collection-valued association
if (isset($nonemptyComponents[$dqlAlias])) {
2009-10-09 18:27:35 +04:00
$collKey = $oid . $relationField;
2012-08-12 04:38:02 +04:00
if (isset($this->initializedCollections[$collKey])) {
$reflFieldValue = $this->initializedCollections[$collKey];
} else if ( ! isset($this->existingCollections[$collKey])) {
2012-08-12 04:52:03 +04:00
$reflFieldValue = $this->initRelatedCollection($parentObject, $parentClass, $relationField, $parentAlias);
}
2012-08-12 21:40:26 +04:00
$indexExists = isset($this->identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]]);
$index = $indexExists ? $this->identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]] : false;
$indexIsValid = $index !== false ? isset($reflFieldValue[$index]) : false;
if ( ! $indexExists || ! $indexIsValid) {
2012-08-12 04:38:02 +04:00
if (isset($this->existingCollections[$collKey])) {
2009-10-19 00:36:02 +04:00
// Collection exists, only look for the element in the identity map.
2012-08-12 04:52:03 +04:00
if ($element = $this->getEntityFromIdentityMap($entityName, $data)) {
2012-08-12 04:38:02 +04:00
$this->resultPointers[$dqlAlias] = $element;
2009-10-09 18:27:35 +04:00
} else {
2012-08-12 04:38:02 +04:00
unset($this->resultPointers[$dqlAlias]);
}
} else {
2012-08-12 04:52:03 +04:00
$element = $this->getEntity($data, $dqlAlias);
2009-10-09 18:27:35 +04:00
if (isset($this->_rsm->indexByMap[$dqlAlias])) {
$indexValue = $row[$this->_rsm->indexByMap[$dqlAlias]];
2009-10-09 18:27:35 +04:00
$reflFieldValue->hydrateSet($indexValue, $element);
2012-08-12 04:38:02 +04:00
$this->identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]] = $indexValue;
2009-10-09 18:27:35 +04:00
} else {
$reflFieldValue->hydrateAdd($element);
$reflFieldValue->last();
2012-08-12 04:38:02 +04:00
$this->identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]] = $reflFieldValue->key();
2009-10-09 18:27:35 +04:00
}
// Update result pointer
2012-08-12 04:38:02 +04:00
$this->resultPointers[$dqlAlias] = $element;
}
2009-10-09 18:27:35 +04:00
} else {
// Update result pointer
2012-08-12 04:38:02 +04:00
$this->resultPointers[$dqlAlias] = $reflFieldValue[$index];
}
} else if ( ! $reflFieldValue) {
2012-08-12 04:52:03 +04:00
$reflFieldValue = $this->initRelatedCollection($parentObject, $parentClass, $relationField, $parentAlias);
} else if ($reflFieldValue instanceof PersistentCollection && $reflFieldValue->isInitialized() === false) {
$reflFieldValue->setInitialized(true);
}
} else {
2009-10-19 00:36:02 +04:00
// PATH B: Single-valued association
2009-10-09 18:27:35 +04:00
$reflFieldValue = $reflField->getValue($parentObject);
if ( ! $reflFieldValue || isset($this->_hints[Query::HINT_REFRESH]) || ($reflFieldValue instanceof Proxy && !$reflFieldValue->__isInitialized__)) {
// we only need to take action if this value is null,
// we refresh the entity or its an unitialized proxy.
2009-07-15 10:46:43 +04:00
if (isset($nonemptyComponents[$dqlAlias])) {
2012-08-12 04:52:03 +04:00
$element = $this->getEntity($data, $dqlAlias);
2009-10-09 18:27:35 +04:00
$reflField->setValue($parentObject, $element);
$this->_uow->setOriginalEntityProperty($oid, $relationField, $element);
2012-08-12 04:38:02 +04:00
$targetClass = $this->ce[$relation['targetEntity']];
if ($relation['isOwningSide']) {
2010-04-10 02:00:36 +04:00
//TODO: Just check hints['fetched'] here?
// If there is an inverse mapping on the target class its bidirectional
if ($relation['inversedBy']) {
$inverseAssoc = $targetClass->associationMappings[$relation['inversedBy']];
if ($inverseAssoc['type'] & ClassMetadata::TO_ONE) {
$targetClass->reflFields[$inverseAssoc['fieldName']]->setValue($element, $parentObject);
$this->_uow->setOriginalEntityProperty(spl_object_hash($element), $inverseAssoc['fieldName'], $parentObject);
}
} else if ($parentClass === $targetClass && $relation['mappedBy']) {
// Special case: bi-directional self-referencing one-one on the same class
2009-10-09 18:27:35 +04:00
$targetClass->reflFields[$relationField]->setValue($element, $parentObject);
}
} else {
// For sure bidirectional, as there is no inverse side in unidirectional mappings
$targetClass->reflFields[$relation['mappedBy']]->setValue($element, $parentObject);
$this->_uow->setOriginalEntityProperty(spl_object_hash($element), $relation['mappedBy'], $parentObject);
}
2009-10-09 18:27:35 +04:00
// Update result pointer
2012-08-12 04:38:02 +04:00
$this->resultPointers[$dqlAlias] = $element;
} else {
$this->_uow->setOriginalEntityProperty($oid, $relationField, null);
$reflField->setValue($parentObject, null);
}
// else leave $reflFieldValue null for single-valued associations
} else {
// Update result pointer
2012-08-12 04:38:02 +04:00
$this->resultPointers[$dqlAlias] = $reflFieldValue;
}
}
} else {
2009-10-19 00:36:02 +04:00
// PATH C: Its a root result element
2012-08-12 04:38:02 +04:00
$this->rootAliases[$dqlAlias] = true; // Mark as root alias
$entityKey = $this->_rsm->entityMappings[$dqlAlias] ?: 0;
// if this row has a NULL value for the root result id then make it a null result.
if ( ! isset($nonemptyComponents[$dqlAlias]) ) {
if ($this->_rsm->isMixed) {
$result[] = array($entityKey => null);
} else {
$result[] = null;
}
2012-08-12 04:38:02 +04:00
$resultKey = $this->resultCounter;
++$this->resultCounter;
continue;
}
// check for existing result from the iterations before
2012-08-12 04:38:02 +04:00
if ( ! isset($this->identifierMap[$dqlAlias][$id[$dqlAlias]])) {
2012-08-12 04:52:03 +04:00
$element = $this->getEntity($rowData[$dqlAlias], $dqlAlias);
2011-10-16 18:13:59 +04:00
if ($this->_rsm->isMixed) {
$element = array($entityKey => $element);
2011-10-16 18:13:59 +04:00
}
if (isset($this->_rsm->indexByMap[$dqlAlias])) {
$resultKey = $row[$this->_rsm->indexByMap[$dqlAlias]];
if (isset($this->_hints['collection'])) {
$this->_hints['collection']->hydrateSet($resultKey, $element);
}
2011-10-16 18:13:59 +04:00
$result[$resultKey] = $element;
} else {
2012-08-12 04:38:02 +04:00
$resultKey = $this->resultCounter;
++$this->resultCounter;
if (isset($this->_hints['collection'])) {
$this->_hints['collection']->hydrateAdd($element);
}
2011-10-16 18:13:59 +04:00
$result[] = $element;
}
2012-08-12 04:38:02 +04:00
$this->identifierMap[$dqlAlias][$id[$dqlAlias]] = $resultKey;
2011-10-16 18:13:59 +04:00
// Update result pointer
2012-08-12 04:38:02 +04:00
$this->resultPointers[$dqlAlias] = $element;
} else {
// Update result pointer
2012-08-12 04:38:02 +04:00
$index = $this->identifierMap[$dqlAlias][$id[$dqlAlias]];
$this->resultPointers[$dqlAlias] = $result[$index];
$resultKey = $index;
/*if ($this->_rsm->isMixed) {
$result[] = $result[$index];
++$this->_resultCounter;
}*/
}
}
}
// Append scalar values to mixed result sets
if (isset($scalars)) {
if ( ! isset($resultKey) ) {
if (isset($this->_rsm->indexByMap['scalars'])) {
$resultKey = $row[$this->_rsm->indexByMap['scalars']];
} else {
2012-08-12 04:38:02 +04:00
$resultKey = $this->resultCounter - 1;
}
}
foreach ($scalars as $name => $value) {
$result[$resultKey][$name] = $value;
}
}
// Append new object to mixed result sets
if (isset($newObjects)) {
if ( ! isset($resultKey) ) {
2012-08-12 04:38:02 +04:00
$resultKey = $this->resultCounter - 1;
}
2012-08-12 04:22:16 +04:00
$count = count($newObjects);
foreach ($newObjects as $objIndex => $newObject) {
2012-01-22 00:16:38 +04:00
$class = $newObject['class'];
2012-08-12 04:22:16 +04:00
$args = $newObject['args'];
$obj = $class->newInstanceArgs($args);
if ($count === 1) {
$result[$resultKey] = $obj;
2012-08-12 04:22:16 +04:00
continue;
}
2012-08-12 04:22:16 +04:00
$result[$resultKey][$objIndex] = $obj;
}
}
}
/**
* When executed in a hydrate() loop we may have to clear internal state to
* decrease memory consumption.
2012-12-02 23:44:56 +04:00
*
* @param mixed $eventArgs
*
2012-12-02 23:44:56 +04:00
* @return void
*/
public function onClear($eventArgs)
{
parent::onClear($eventArgs);
2012-08-12 21:40:26 +04:00
$aliases = array_keys($this->identifierMap);
2012-08-12 04:38:02 +04:00
$this->identifierMap = array();
foreach ($aliases as $alias) {
2012-08-12 04:38:02 +04:00
$this->identifierMap[$alias] = array();
}
}
}