2009-01-14 00:56:43 +03:00
|
|
|
<?php
|
2009-02-07 20:02:13 +03:00
|
|
|
/*
|
|
|
|
* 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
|
2009-02-07 20:02:13 +03:00
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
2009-01-15 16:30:44 +03:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\ORM\Internal\Hydration;
|
|
|
|
|
2010-03-29 17:20:41 +04:00
|
|
|
use PDO,
|
2010-08-09 15:13:21 +04:00
|
|
|
Doctrine\ORM\Mapping\ClassMetadata,
|
2010-03-29 17:20:41 +04:00
|
|
|
Doctrine\ORM\PersistentCollection,
|
2009-07-29 15:57:27 +04:00
|
|
|
Doctrine\ORM\Query,
|
2011-11-29 19:36:32 +04:00
|
|
|
Doctrine\ORM\Event\LifecycleEventArgs,
|
|
|
|
Doctrine\ORM\Events,
|
2009-07-29 15:57:27 +04:00
|
|
|
Doctrine\Common\Collections\ArrayCollection,
|
2011-12-16 02:00:01 +04:00
|
|
|
Doctrine\Common\Collections\Collection,
|
|
|
|
Doctrine\ORM\Proxy\Proxy;
|
2009-01-14 00:56:43 +03:00
|
|
|
|
|
|
|
/**
|
2009-02-07 20:02:13 +03:00
|
|
|
* The ObjectHydrator constructs an object graph out of an SQL result set.
|
2009-01-14 00:56:43 +03:00
|
|
|
*
|
2011-11-03 04:08:24 +04:00
|
|
|
* @since 2.0
|
2009-05-13 19:19:27 +04:00
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2011-11-03 04:08:24 +04:00
|
|
|
* @author Guilherme Blanco <guilhermeblanoc@hotmail.com>
|
2011-11-14 07:36:39 +04:00
|
|
|
*
|
2009-10-09 18:27:35 +04:00
|
|
|
* @internal Highly performance-sensitive code.
|
2009-01-14 00:56:43 +03:00
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
class ObjectHydrator extends AbstractHydrator
|
2009-01-14 00:56:43 +03:00
|
|
|
{
|
2009-08-28 14:48:40 +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.
|
2009-05-14 22:34:12 +04:00
|
|
|
*/
|
2009-05-17 23:27:12 +04:00
|
|
|
private $_ce = array();
|
2011-05-01 02:17:40 +04:00
|
|
|
|
2009-08-28 14:48:40 +04:00
|
|
|
/* The following parts are reinitialized on every hydration run. */
|
2011-05-01 02:17:40 +04:00
|
|
|
|
2009-08-28 14:48:40 +04:00
|
|
|
private $_identifierMap;
|
|
|
|
private $_resultPointers;
|
|
|
|
private $_idTemplate;
|
2009-07-28 20:36:24 +04:00
|
|
|
private $_resultCounter;
|
2009-08-28 14:48:40 +04:00
|
|
|
private $_rootAliases = array();
|
2009-07-29 15:57:27 +04:00
|
|
|
private $_initializedCollections = array();
|
2009-10-09 18:27:35 +04:00
|
|
|
private $_existingCollections = array();
|
2011-05-01 02:17:40 +04:00
|
|
|
|
2009-01-14 00:56:43 +03:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
/** @override */
|
2011-11-03 04:08:24 +04:00
|
|
|
protected function prepare()
|
2009-01-14 00:56:43 +03:00
|
|
|
{
|
2009-07-28 20:36:24 +04:00
|
|
|
$this->_identifierMap =
|
|
|
|
$this->_resultPointers =
|
2009-10-22 16:50:58 +04:00
|
|
|
$this->_idTemplate = array();
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2009-07-28 20:36:24 +04:00
|
|
|
$this->_resultCounter = 0;
|
2011-11-14 07:36:39 +04:00
|
|
|
|
|
|
|
if ( ! isset($this->_hints['deferEagerLoad'])) {
|
2011-03-06 23:26:54 +03:00
|
|
|
$this->_hints['deferEagerLoad'] = true;
|
|
|
|
}
|
2011-10-29 01:45:03 +04:00
|
|
|
|
2009-05-21 23:18:14 +04:00
|
|
|
foreach ($this->_rsm->aliasMap as $dqlAlias => $className) {
|
2009-01-14 00:56:43 +03:00
|
|
|
$this->_identifierMap[$dqlAlias] = array();
|
2011-11-14 07:36:39 +04:00
|
|
|
$this->_idTemplate[$dqlAlias] = '';
|
2009-05-14 22:34:12 +04:00
|
|
|
|
2009-07-29 15:57:27 +04:00
|
|
|
if ( ! isset($this->_ce[$className])) {
|
2011-11-14 07:36:39 +04:00
|
|
|
$this->_ce[$className] = $this->_em->getClassMetadata($className);
|
2009-04-12 23:02:12 +04:00
|
|
|
}
|
2011-05-01 02:17:40 +04:00
|
|
|
|
2009-08-28 14:48:40 +04:00
|
|
|
// Remember which associations are "fetch joined", so that we know where to inject
|
|
|
|
// collection stubs or proxies and where not.
|
2011-11-14 07:36:39 +04:00
|
|
|
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]];
|
|
|
|
$sourceClass = $this->_getClassMetadata($sourceClassName);
|
|
|
|
$assoc = $sourceClass->associationMappings[$this->_rsm->relationMap[$dqlAlias]];
|
|
|
|
|
2011-11-15 02:05:33 +04:00
|
|
|
$this->_hints['fetched'][$this->_rsm->parentAliasMap[$dqlAlias]][$assoc['fieldName']] = true;
|
2011-05-01 02:17:40 +04:00
|
|
|
|
2011-11-14 07:36:39 +04:00
|
|
|
if ($assoc['type'] === ClassMetadata::MANY_TO_MANY) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark any non-collection opposite sides as fetched, too.
|
|
|
|
if ($assoc['mappedBy']) {
|
2011-11-15 02:05:33 +04:00
|
|
|
$this->_hints['fetched'][$dqlAlias][$assoc['mappedBy']] = true;
|
2011-11-14 07:36:39 +04:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-11-15 02:05:33 +04:00
|
|
|
// handle fetch-joined owning side bi-directional one-to-one associations
|
2011-11-14 07:36:39 +04:00
|
|
|
if ($assoc['inversedBy']) {
|
|
|
|
$class = $this->_ce[$className];
|
|
|
|
$inverseAssoc = $class->associationMappings[$assoc['inversedBy']];
|
|
|
|
|
|
|
|
if ( ! ($inverseAssoc['type'] & ClassMetadata::TO_ONE)) {
|
|
|
|
continue;
|
2010-01-30 00:24:29 +03:00
|
|
|
}
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2011-11-15 02:05:33 +04:00
|
|
|
$this->_hints['fetched'][$dqlAlias][$inverseAssoc['fieldName']] = true;
|
2009-05-13 19:19:27 +04:00
|
|
|
}
|
2009-01-14 00:56:43 +03:00
|
|
|
}
|
|
|
|
}
|
2010-03-29 17:20:41 +04:00
|
|
|
|
2009-08-28 14:48:40 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2011-11-03 04:08:24 +04:00
|
|
|
protected function cleanup()
|
2009-08-11 14:51:38 +04:00
|
|
|
{
|
2011-03-06 23:26:54 +03:00
|
|
|
$eagerLoad = (isset($this->_hints['deferEagerLoad'])) && $this->_hints['deferEagerLoad'] == true;
|
2011-10-29 01:45:03 +04:00
|
|
|
|
2011-11-03 04:08:24 +04:00
|
|
|
parent::cleanup();
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
$this->_identifierMap =
|
2009-10-09 18:27:35 +04:00
|
|
|
$this->_initializedCollections =
|
|
|
|
$this->_existingCollections =
|
2009-08-11 14:51:38 +04:00
|
|
|
$this->_resultPointers = array();
|
2011-10-29 01:45:03 +04:00
|
|
|
|
2011-03-06 23:26:54 +03:00
|
|
|
if ($eagerLoad) {
|
|
|
|
$this->_em->getUnitOfWork()->triggerEagerLoads();
|
|
|
|
}
|
2009-08-11 14:51:38 +04:00
|
|
|
}
|
2009-01-14 00:56:43 +03:00
|
|
|
|
2009-01-15 16:30:44 +03:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2011-11-03 04:08:24 +04:00
|
|
|
protected function hydrateAllData()
|
2009-01-14 00:56:43 +03:00
|
|
|
{
|
2009-08-03 17:25:56 +04:00
|
|
|
$result = array();
|
2011-11-14 07:36:39 +04:00
|
|
|
$cache = array();
|
2010-03-29 17:20:41 +04:00
|
|
|
|
|
|
|
while ($row = $this->_stmt->fetch(PDO::FETCH_ASSOC)) {
|
2011-11-03 04:08:24 +04:00
|
|
|
$this->hydrateRowData($row, $cache, $result);
|
2009-01-14 00:56:43 +03:00
|
|
|
}
|
|
|
|
|
2009-10-09 18:27:35 +04:00
|
|
|
// Take snapshots from all newly initialized collections
|
2009-07-29 15:57:27 +04:00
|
|
|
foreach ($this->_initializedCollections as $coll) {
|
2009-02-07 20:02:13 +03:00
|
|
|
$coll->takeSnapshot();
|
2009-01-14 00:56:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2009-04-12 23:02:12 +04:00
|
|
|
/**
|
2009-07-20 14:52:07 +04:00
|
|
|
* Initializes a related collection.
|
2009-04-12 23:02:12 +04:00
|
|
|
*
|
2012-07-12 02:44:07 +04:00
|
|
|
* @param object $entity The entity to which the collection belongs.
|
2011-11-15 02:05:33 +04:00
|
|
|
* @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.
|
2009-04-12 23:02:12 +04:00
|
|
|
*/
|
2011-11-15 02:05:33 +04:00
|
|
|
private function _initRelatedCollection($entity, $class, $fieldName, $parentDqlAlias)
|
2009-01-14 00:56:43 +03:00
|
|
|
{
|
2011-11-14 07:36:39 +04:00
|
|
|
$oid = spl_object_hash($entity);
|
2009-12-07 23:35:44 +03:00
|
|
|
$relation = $class->associationMappings[$fieldName];
|
2011-11-14 07:36:39 +04:00
|
|
|
$value = $class->reflFields[$fieldName]->getValue($entity);
|
2010-10-31 13:07:26 +03:00
|
|
|
|
2009-09-06 19:25:23 +04:00
|
|
|
if ($value === null) {
|
|
|
|
$value = new ArrayCollection;
|
|
|
|
}
|
2010-10-31 13:07:26 +03:00
|
|
|
|
2009-12-07 15:47:23 +03:00
|
|
|
if ( ! $value instanceof PersistentCollection) {
|
2009-09-06 19:25:23 +04:00
|
|
|
$value = new PersistentCollection(
|
2011-11-14 07:36:39 +04:00
|
|
|
$this->_em, $this->_ce[$relation['targetEntity']], $value
|
2009-09-06 19:25:23 +04:00
|
|
|
);
|
|
|
|
$value->setOwner($entity, $relation);
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2009-12-07 23:35:44 +03:00
|
|
|
$class->reflFields[$fieldName]->setValue($entity, $value);
|
|
|
|
$this->_uow->setOriginalEntityProperty($oid, $fieldName, $value);
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2009-12-07 23:35:44 +03:00
|
|
|
$this->_initializedCollections[$oid . $fieldName] = $value;
|
2011-11-14 07:36:39 +04:00
|
|
|
} else if (
|
|
|
|
isset($this->_hints[Query::HINT_REFRESH]) ||
|
2011-11-15 02:05:33 +04:00
|
|
|
isset($this->_hints['fetched'][$parentDqlAlias][$fieldName]) &&
|
2011-11-14 07:36:39 +04:00
|
|
|
! $value->isInitialized()
|
|
|
|
) {
|
2010-10-31 13:07:26 +03:00
|
|
|
// 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);
|
2010-02-25 18:47:20 +03:00
|
|
|
$value->unwrap()->clear();
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2009-12-07 23:35:44 +03:00
|
|
|
$this->_initializedCollections[$oid . $fieldName] = $value;
|
2009-10-09 18:27:35 +04:00
|
|
|
} else {
|
2010-10-31 13:07:26 +03:00
|
|
|
// Is already PersistentCollection, and DON'T REFRESH or FETCH-JOIN!
|
2009-12-07 23:35:44 +03:00
|
|
|
$this->_existingCollections[$oid . $fieldName] = $value;
|
2009-09-06 19:25:23 +04:00
|
|
|
}
|
2010-10-31 13:07:26 +03:00
|
|
|
|
2009-09-06 19:25:23 +04:00
|
|
|
return $value;
|
2009-01-14 00:56:43 +03:00
|
|
|
}
|
2011-05-01 02:17:40 +04:00
|
|
|
|
2009-07-21 13:25:14 +04:00
|
|
|
/**
|
|
|
|
* Gets an entity instance.
|
2011-10-29 01:45:03 +04:00
|
|
|
*
|
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.
|
2009-07-21 13:25:14 +04:00
|
|
|
* @return object The entity.
|
|
|
|
*/
|
2009-08-28 14:48:40 +04:00
|
|
|
private function _getEntity(array $data, $dqlAlias)
|
2009-01-14 00:56:43 +03:00
|
|
|
{
|
2011-05-01 02:17:40 +04:00
|
|
|
$className = $this->_rsm->aliasMap[$dqlAlias];
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2009-05-26 15:30:07 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2009-07-20 19:30:54 +04:00
|
|
|
$discrColumn = $this->_rsm->metaMappings[$this->_rsm->discriminatorColumns[$dqlAlias]];
|
2011-11-01 02:35:41 +04:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-11-01 02:35:41 +04:00
|
|
|
if ($data[$discrColumn] === "") {
|
|
|
|
throw HydrationException::emptyDiscriminatorValue($dqlAlias);
|
|
|
|
}
|
|
|
|
|
2009-08-17 21:58:16 +04:00
|
|
|
$className = $this->_ce[$className]->discriminatorMap[$data[$discrColumn]];
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2009-04-12 23:02:12 +04:00
|
|
|
unset($data[$discrColumn]);
|
|
|
|
}
|
2011-10-29 01:45:03 +04:00
|
|
|
|
2011-07-05 01:19:08 +04:00
|
|
|
if (isset($this->_hints[Query::HINT_REFRESH_ENTITY]) && isset($this->_rootAliases[$dqlAlias])) {
|
2011-11-14 07:36:39 +04:00
|
|
|
$this->registerManaged($this->_ce[$className], $this->_hints[Query::HINT_REFRESH_ENTITY], $data);
|
2011-07-05 01:19:08 +04:00
|
|
|
}
|
2011-10-29 01:45:03 +04:00
|
|
|
|
2011-11-15 02:05:33 +04:00
|
|
|
$this->_hints['fetchAlias'] = $dqlAlias;
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2012-02-18 02:26:42 +04:00
|
|
|
return $this->_uow->createEntity($className, $data, $this->_hints);
|
2009-01-14 00:56:43 +03:00
|
|
|
}
|
2011-05-01 02:17:40 +04:00
|
|
|
|
2012-07-12 02:44:07 +04:00
|
|
|
/**
|
|
|
|
* @param string $className
|
|
|
|
* @param array $data
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2009-10-09 18:27:35 +04:00
|
|
|
private function _getEntityFromIdentityMap($className, array $data)
|
|
|
|
{
|
2011-08-06 22:23:47 +04:00
|
|
|
// TODO: Abstract this code and UnitOfWork::createEntity() equivalent?
|
2009-10-09 18:27:35 +04:00
|
|
|
$class = $this->_ce[$className];
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2011-08-06 22:23:47 +04:00
|
|
|
/* @var $class ClassMetadata */
|
2009-10-09 18:27:35 +04:00
|
|
|
if ($class->isIdentifierComposite) {
|
|
|
|
$idHash = '';
|
|
|
|
foreach ($class->identifier as $fieldName) {
|
2011-08-06 22:23:47 +04:00
|
|
|
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
|
|
|
}
|
2009-10-15 23:03:27 +04:00
|
|
|
return $this->_uow->tryGetByIdHash(rtrim($idHash), $class->rootEntityName);
|
2011-08-06 22:23:47 +04:00
|
|
|
} 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 {
|
2009-10-15 23:03:27 +04:00
|
|
|
return $this->_uow->tryGetByIdHash($data[$class->identifier[0]], $class->rootEntityName);
|
2009-10-09 18:27:35 +04:00
|
|
|
}
|
|
|
|
}
|
2011-05-01 02:17:40 +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.
|
2011-10-29 01:45:03 +04:00
|
|
|
*
|
2009-07-28 20:36:24 +04:00
|
|
|
* @param string $className The name of the class.
|
|
|
|
* @return ClassMetadata
|
|
|
|
*/
|
|
|
|
private function _getClassMetadata($className)
|
|
|
|
{
|
|
|
|
if ( ! isset($this->_ce[$className])) {
|
|
|
|
$this->_ce[$className] = $this->_em->getClassMetadata($className);
|
|
|
|
}
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2009-07-28 20:36:24 +04:00
|
|
|
return $this->_ce[$className];
|
|
|
|
}
|
2009-01-14 00:56:43 +03:00
|
|
|
|
|
|
|
/**
|
2009-10-19 00:36:02 +04:00
|
|
|
* Hydrates a single row in an SQL result set.
|
2011-10-29 01:45:03 +04:00
|
|
|
*
|
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:
|
2011-10-29 01:45:03 +04:00
|
|
|
*
|
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
|
2011-10-29 01:45:03 +04:00
|
|
|
* 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.
|
2010-04-26 15:02:30 +04:00
|
|
|
* @param array $result The result array to fill.
|
2009-01-14 00:56:43 +03:00
|
|
|
*/
|
2011-11-03 04:08:24 +04:00
|
|
|
protected function hydrateRowData(array $row, array &$cache, array &$result)
|
2009-01-14 00:56:43 +03:00
|
|
|
{
|
2009-08-28 14:48:40 +04:00
|
|
|
// Initialize
|
2009-01-14 00:56:43 +03: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.
|
2011-11-03 04:08:24 +04:00
|
|
|
$rowData = $this->gatherRowData($row, $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'];
|
2011-11-15 07:09:48 +04:00
|
|
|
|
2009-01-14 00:56:43 +03:00
|
|
|
unset($rowData['scalars']);
|
2011-11-15 07:09:48 +04:00
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
if (empty($rowData)) {
|
|
|
|
++$this->_resultCounter;
|
|
|
|
}
|
2009-01-14 00:56:43 +03:00
|
|
|
}
|
|
|
|
|
2009-10-19 00:36:02 +04:00
|
|
|
// Hydrate the data chunks
|
2009-01-14 00:56:43 +03:00
|
|
|
foreach ($rowData as $dqlAlias => $data) {
|
2009-05-21 23:18:14 +04:00
|
|
|
$entityName = $this->_rsm->aliasMap[$dqlAlias];
|
2011-12-11 19:11:16 +04:00
|
|
|
|
2009-05-14 22:34:12 +04:00
|
|
|
if (isset($this->_rsm->parentAliasMap[$dqlAlias])) {
|
2011-12-11 19:11:16 +04:00
|
|
|
// It's a joined result
|
2010-03-29 17:20:41 +04:00
|
|
|
|
2009-10-09 18:27:35 +04:00
|
|
|
$parentAlias = $this->_rsm->parentAliasMap[$dqlAlias];
|
2010-09-21 02:32:07 +04:00
|
|
|
// we need the $path to save into the identifier map which entities were already
|
|
|
|
// seen for this parent-child relationship
|
|
|
|
$path = $parentAlias . '.' . $dqlAlias;
|
2009-04-09 22:12:48 +04:00
|
|
|
|
2011-10-16 13:08:11 +04:00
|
|
|
// 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])) {
|
2011-10-16 13:08:11 +04:00
|
|
|
// 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.
|
|
|
|
if ($this->_rsm->isMixed && isset($this->_rootAliases[$parentAlias])) {
|
2011-05-01 02:17:40 +04:00
|
|
|
$first = reset($this->_resultPointers);
|
2011-11-15 07:10:27 +04:00
|
|
|
$parentObject = $first[key($first)];
|
2009-10-09 18:27:35 +04:00
|
|
|
} else if (isset($this->_resultPointers[$parentAlias])) {
|
|
|
|
$parentObject = $this->_resultPointers[$parentAlias];
|
2009-04-09 22:12:48 +04:00
|
|
|
} else {
|
2009-10-09 18:27:35 +04:00
|
|
|
// Parent object of relation not found, so skip it.
|
|
|
|
continue;
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
2009-01-14 00:56:43 +03:00
|
|
|
|
2009-12-07 23:35:44 +03:00
|
|
|
$parentClass = $this->_ce[$this->_rsm->aliasMap[$parentAlias]];
|
2009-10-09 18:27:35 +04:00
|
|
|
$oid = spl_object_hash($parentObject);
|
2009-08-11 14:51:38 +04:00
|
|
|
$relationField = $this->_rsm->relationMap[$dqlAlias];
|
2009-12-07 23:35:44 +03:00
|
|
|
$relation = $parentClass->associationMappings[$relationField];
|
|
|
|
$reflField = $parentClass->reflFields[$relationField];
|
2010-08-09 15:13:21 +04:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
// Check the type of the relation (many or single-valued)
|
2010-08-09 15:13:21 +04:00
|
|
|
if ( ! ($relation['type'] & ClassMetadata::TO_ONE)) {
|
2012-01-28 15:25:01 +04:00
|
|
|
$reflFieldValue = $reflField->getValue($parentObject);
|
2009-10-19 00:36:02 +04:00
|
|
|
// PATH A: Collection-valued association
|
2009-04-09 22:12:48 +04:00
|
|
|
if (isset($nonemptyComponents[$dqlAlias])) {
|
2009-10-09 18:27:35 +04:00
|
|
|
$collKey = $oid . $relationField;
|
|
|
|
if (isset($this->_initializedCollections[$collKey])) {
|
|
|
|
$reflFieldValue = $this->_initializedCollections[$collKey];
|
|
|
|
} else if ( ! isset($this->_existingCollections[$collKey])) {
|
2011-11-15 02:05:33 +04:00
|
|
|
$reflFieldValue = $this->_initRelatedCollection($parentObject, $parentClass, $relationField, $parentAlias);
|
2009-05-07 20:36:27 +04:00
|
|
|
}
|
2011-05-01 02:17:40 +04:00
|
|
|
|
2010-09-21 02:32:07 +04:00
|
|
|
$indexExists = isset($this->_identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]]);
|
|
|
|
$index = $indexExists ? $this->_identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]] : false;
|
2009-08-28 14:48:40 +04:00
|
|
|
$indexIsValid = $index !== false ? isset($reflFieldValue[$index]) : false;
|
2011-05-01 02:17:40 +04:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
if ( ! $indexExists || ! $indexIsValid) {
|
2009-10-09 18:27:35 +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.
|
2009-10-09 18:27:35 +04:00
|
|
|
if ($element = $this->_getEntityFromIdentityMap($entityName, $data)) {
|
|
|
|
$this->_resultPointers[$dqlAlias] = $element;
|
|
|
|
} else {
|
|
|
|
unset($this->_resultPointers[$dqlAlias]);
|
2009-05-07 20:36:27 +04:00
|
|
|
}
|
2009-04-09 22:12:48 +04:00
|
|
|
} else {
|
2009-10-09 18:27:35 +04:00
|
|
|
$element = $this->_getEntity($data, $dqlAlias);
|
2010-02-27 20:48:18 +03:00
|
|
|
|
2009-10-09 18:27:35 +04:00
|
|
|
if (isset($this->_rsm->indexByMap[$dqlAlias])) {
|
2011-10-16 21:23:20 +04:00
|
|
|
$indexValue = $row[$this->_rsm->indexByMap[$dqlAlias]];
|
2009-10-09 18:27:35 +04:00
|
|
|
$reflFieldValue->hydrateSet($indexValue, $element);
|
2010-09-21 02:32:07 +04:00
|
|
|
$this->_identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]] = $indexValue;
|
2009-10-09 18:27:35 +04:00
|
|
|
} else {
|
|
|
|
$reflFieldValue->hydrateAdd($element);
|
|
|
|
$reflFieldValue->last();
|
2010-09-21 02:32:07 +04:00
|
|
|
$this->_identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]] = $reflFieldValue->key();
|
2009-10-09 18:27:35 +04:00
|
|
|
}
|
|
|
|
// Update result pointer
|
|
|
|
$this->_resultPointers[$dqlAlias] = $element;
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
2009-10-09 18:27:35 +04:00
|
|
|
} else {
|
|
|
|
// Update result pointer
|
|
|
|
$this->_resultPointers[$dqlAlias] = $reflFieldValue[$index];
|
2009-01-14 00:56:43 +03:00
|
|
|
}
|
2012-01-28 15:25:01 +04:00
|
|
|
} else if ( ! $reflFieldValue) {
|
2011-11-15 02:05:33 +04:00
|
|
|
$reflFieldValue = $this->_initRelatedCollection($parentObject, $parentClass, $relationField, $parentAlias);
|
2012-01-28 15:25:01 +04:00
|
|
|
} else if ($reflFieldValue instanceof PersistentCollection && $reflFieldValue->isInitialized() === false) {
|
|
|
|
$reflFieldValue->setInitialized(true);
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
2012-01-28 15:25:01 +04:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
} 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);
|
2011-12-16 02:00:01 +04:00
|
|
|
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])) {
|
2009-08-28 14:48:40 +04:00
|
|
|
$element = $this->_getEntity($data, $dqlAlias);
|
2009-10-09 18:27:35 +04:00
|
|
|
$reflField->setValue($parentObject, $element);
|
2009-07-29 15:57:27 +04:00
|
|
|
$this->_uow->setOriginalEntityProperty($oid, $relationField, $element);
|
2010-08-09 15:13:21 +04:00
|
|
|
$targetClass = $this->_ce[$relation['targetEntity']];
|
2011-11-14 07:36:39 +04:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($relation['isOwningSide']) {
|
2010-04-10 02:00:36 +04:00
|
|
|
//TODO: Just check hints['fetched'] here?
|
2009-07-29 15:57:27 +04:00
|
|
|
// If there is an inverse mapping on the target class its bidirectional
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($relation['inversedBy']) {
|
|
|
|
$inverseAssoc = $targetClass->associationMappings[$relation['inversedBy']];
|
2010-08-09 21:34:36 +04:00
|
|
|
if ($inverseAssoc['type'] & ClassMetadata::TO_ONE) {
|
2010-08-09 15:13:21 +04:00
|
|
|
$targetClass->reflFields[$inverseAssoc['fieldName']]->setValue($element, $parentObject);
|
|
|
|
$this->_uow->setOriginalEntityProperty(spl_object_hash($element), $inverseAssoc['fieldName'], $parentObject);
|
2009-10-16 14:35:46 +04:00
|
|
|
}
|
2010-08-09 15:13:21 +04:00
|
|
|
} else if ($parentClass === $targetClass && $relation['mappedBy']) {
|
2009-07-29 15:57:27 +04:00
|
|
|
// 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);
|
2009-07-29 15:57:27 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// For sure bidirectional, as there is no inverse side in unidirectional mappings
|
2010-08-09 15:13:21 +04:00
|
|
|
$targetClass->reflFields[$relation['mappedBy']]->setValue($element, $parentObject);
|
|
|
|
$this->_uow->setOriginalEntityProperty(spl_object_hash($element), $relation['mappedBy'], $parentObject);
|
2009-07-29 15:57:27 +04:00
|
|
|
}
|
2009-10-09 18:27:35 +04:00
|
|
|
// Update result pointer
|
|
|
|
$this->_resultPointers[$dqlAlias] = $element;
|
2011-12-19 20:55:19 +04:00
|
|
|
} else {
|
|
|
|
$this->_uow->setOriginalEntityProperty($oid, $relationField, null);
|
2009-05-17 23:27:12 +04:00
|
|
|
}
|
2009-08-28 14:48:40 +04:00
|
|
|
// else leave $reflFieldValue null for single-valued associations
|
|
|
|
} else {
|
|
|
|
// Update result pointer
|
2009-08-03 17:25:56 +04:00
|
|
|
$this->_resultPointers[$dqlAlias] = $reflFieldValue;
|
2009-07-29 15:57:27 +04:00
|
|
|
}
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
|
|
|
} else {
|
2009-10-19 00:36:02 +04:00
|
|
|
// PATH C: Its a root result element
|
2009-04-09 22:12:48 +04:00
|
|
|
$this->_rootAliases[$dqlAlias] = true; // Mark as root alias
|
2011-11-14 07:36:39 +04:00
|
|
|
$entityKey = $this->_rsm->entityMappings[$dqlAlias] ?: 0;
|
2009-04-09 22:12:48 +04:00
|
|
|
|
2011-10-16 13:08:11 +04:00
|
|
|
// 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) {
|
2011-11-14 07:36:39 +04:00
|
|
|
$result[] = array($entityKey => null);
|
2011-10-16 13:08:11 +04:00
|
|
|
} else {
|
|
|
|
$result[] = null;
|
|
|
|
}
|
2011-10-16 21:23:20 +04:00
|
|
|
$resultKey = $this->_resultCounter;
|
2011-10-16 13:08:11 +04:00
|
|
|
++$this->_resultCounter;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for existing result from the iterations before
|
2009-08-28 14:48:40 +04:00
|
|
|
if ( ! isset($this->_identifierMap[$dqlAlias][$id[$dqlAlias]])) {
|
|
|
|
$element = $this->_getEntity($rowData[$dqlAlias], $dqlAlias);
|
2011-10-16 18:13:59 +04:00
|
|
|
if ($this->_rsm->isMixed) {
|
2011-11-14 07:36:39 +04:00
|
|
|
$element = array($entityKey => $element);
|
2011-10-16 18:13:59 +04:00
|
|
|
}
|
|
|
|
|
2009-07-20 14:52:07 +04:00
|
|
|
if (isset($this->_rsm->indexByMap[$dqlAlias])) {
|
2011-10-16 21:23:20 +04:00
|
|
|
$resultKey = $row[$this->_rsm->indexByMap[$dqlAlias]];
|
2011-03-13 02:15:50 +03:00
|
|
|
|
|
|
|
if (isset($this->_hints['collection'])) {
|
2011-10-16 14:46:17 +04:00
|
|
|
$this->_hints['collection']->hydrateSet($resultKey, $element);
|
2011-03-13 02:15:50 +03:00
|
|
|
}
|
2011-10-16 18:13:59 +04:00
|
|
|
|
|
|
|
$result[$resultKey] = $element;
|
2009-04-09 22:12:48 +04:00
|
|
|
} else {
|
2011-10-16 18:13:59 +04:00
|
|
|
$resultKey = $this->_resultCounter;
|
2009-08-31 20:21:29 +04:00
|
|
|
++$this->_resultCounter;
|
2011-03-13 02:15:50 +03:00
|
|
|
|
|
|
|
if (isset($this->_hints['collection'])) {
|
|
|
|
$this->_hints['collection']->hydrateAdd($element);
|
|
|
|
}
|
2011-10-16 18:13:59 +04:00
|
|
|
|
|
|
|
$result[] = $element;
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2011-10-16 18:13:59 +04:00
|
|
|
$this->_identifierMap[$dqlAlias][$id[$dqlAlias]] = $resultKey;
|
|
|
|
|
2009-08-03 17:25:56 +04:00
|
|
|
// Update result pointer
|
2009-08-28 14:48:40 +04:00
|
|
|
$this->_resultPointers[$dqlAlias] = $element;
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
} else {
|
2009-08-03 17:25:56 +04:00
|
|
|
// Update result pointer
|
2009-04-09 22:12:48 +04:00
|
|
|
$index = $this->_identifierMap[$dqlAlias][$id[$dqlAlias]];
|
2009-08-03 17:25:56 +04:00
|
|
|
$this->_resultPointers[$dqlAlias] = $result[$index];
|
2011-10-16 14:46:17 +04:00
|
|
|
$resultKey = $index;
|
2010-02-20 00:28:17 +03:00
|
|
|
/*if ($this->_rsm->isMixed) {
|
|
|
|
$result[] = $result[$index];
|
|
|
|
++$this->_resultCounter;
|
|
|
|
}*/
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
2009-01-14 00:56:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append scalar values to mixed result sets
|
|
|
|
if (isset($scalars)) {
|
2011-10-16 14:46:17 +04:00
|
|
|
if ( ! isset($resultKey) ) {
|
2011-10-16 21:23:20 +04:00
|
|
|
if (isset($this->_rsm->indexByMap['scalars'])) {
|
|
|
|
$resultKey = $row[$this->_rsm->indexByMap['scalars']];
|
|
|
|
} else {
|
|
|
|
$resultKey = $this->_resultCounter - 1;
|
|
|
|
}
|
2011-10-16 14:46:17 +04:00
|
|
|
}
|
|
|
|
|
2009-01-14 00:56:43 +03:00
|
|
|
foreach ($scalars as $name => $value) {
|
2011-10-16 14:46:17 +04:00
|
|
|
$result[$resultKey][$name] = $value;
|
2009-01-14 00:56:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-27 14:00:43 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* When executed in a hydrate() loop we may have to clear internal state to
|
|
|
|
* decrease memory consumption.
|
|
|
|
*/
|
|
|
|
public function onClear($eventArgs)
|
|
|
|
{
|
|
|
|
parent::onClear($eventArgs);
|
|
|
|
|
|
|
|
$aliases = array_keys($this->_identifierMap);
|
|
|
|
$this->_identifierMap = array();
|
|
|
|
|
|
|
|
foreach ($aliases as $alias) {
|
|
|
|
$this->_identifierMap[$alias] = array();
|
|
|
|
}
|
|
|
|
}
|
2009-07-20 16:05:19 +04:00
|
|
|
}
|