1
0
mirror of synced 2024-12-13 14:56:01 +03:00

fix some cs on ObjectHydrator

This commit is contained in:
Fabio B. Silva 2012-08-11 21:38:02 -03:00
parent 4dca27962e
commit 91efe10855

View File

@ -19,15 +19,12 @@
namespace Doctrine\ORM\Internal\Hydration; namespace Doctrine\ORM\Internal\Hydration;
use PDO, use PDO;
Doctrine\ORM\Mapping\ClassMetadata, use Doctrine\ORM\Mapping\ClassMetadata;
Doctrine\ORM\PersistentCollection, use Doctrine\ORM\PersistentCollection;
Doctrine\ORM\Query, use Doctrine\ORM\Query;
Doctrine\ORM\Event\LifecycleEventArgs, use Doctrine\Common\Collections\ArrayCollection;
Doctrine\ORM\Events, use Doctrine\ORM\Proxy\Proxy;
Doctrine\Common\Collections\ArrayCollection,
Doctrine\Common\Collections\Collection,
Doctrine\ORM\Proxy\Proxy;
/** /**
* The ObjectHydrator constructs an object graph out of an SQL result set. * The ObjectHydrator constructs an object graph out of an SQL result set.
@ -35,6 +32,7 @@ use PDO,
* @since 2.0 * @since 2.0
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
* @author Guilherme Blanco <guilhermeblanoc@hotmail.com> * @author Guilherme Blanco <guilhermeblanoc@hotmail.com>
* @author Fabio B. Silva <fabio.bat.silva@gmail.com>
* *
* @internal Highly performance-sensitive code. * @internal Highly performance-sensitive code.
*/ */
@ -43,38 +41,67 @@ class ObjectHydrator extends AbstractHydrator
/* Local ClassMetadata cache to avoid going to the EntityManager all the time. /* Local ClassMetadata cache to avoid going to the EntityManager all the time.
* This local cache is maintained between hydration runs and not cleared. * This local cache is maintained between hydration runs and not cleared.
*/ */
private $_ce = array(); private $ce = array();
/* The following parts are reinitialized on every hydration run. */ /* The following parts are reinitialized on every hydration run. */
private $_identifierMap; /**
private $_resultPointers; * @var array
private $_idTemplate; */
private $_resultCounter; private $identifierMap;
private $_rootAliases = array();
private $_initializedCollections = array(); /**
private $_existingCollections = array(); * @var array
*/
private $resultPointers;
/**
* @var array
*/
private $idTemplate;
/**
* @var integer
*/
private $resultCounter;
/**
* @var array
*/
private $rootAliases = array();
/**
* @var array
*/
private $initializedCollections = array();
/**
* @var array
*/
private $existingCollections = array();
/** @override */ /**
* @override
*/
protected function prepare() protected function prepare()
{ {
$this->_identifierMap = $this->identifierMap =
$this->_resultPointers = $this->resultPointers =
$this->_idTemplate = array(); $this->idTemplate = array();
$this->_resultCounter = 0; $this->resultCounter = 0;
if ( ! isset($this->_hints['deferEagerLoad'])) { if ( ! isset($this->_hints['deferEagerLoad'])) {
$this->_hints['deferEagerLoad'] = true; $this->_hints['deferEagerLoad'] = true;
} }
foreach ($this->_rsm->aliasMap as $dqlAlias => $className) { foreach ($this->_rsm->aliasMap as $dqlAlias => $className) {
$this->_identifierMap[$dqlAlias] = array(); $this->identifierMap[$dqlAlias] = array();
$this->_idTemplate[$dqlAlias] = ''; $this->idTemplate[$dqlAlias] = '';
if ( ! isset($this->_ce[$className])) { if ( ! isset($this->ce[$className])) {
$this->_ce[$className] = $this->_em->getClassMetadata($className); $this->ce[$className] = $this->_em->getClassMetadata($className);
} }
// Remember which associations are "fetch joined", so that we know where to inject // Remember which associations are "fetch joined", so that we know where to inject
@ -106,7 +133,7 @@ class ObjectHydrator extends AbstractHydrator
// handle fetch-joined owning side bi-directional one-to-one associations // handle fetch-joined owning side bi-directional one-to-one associations
if ($assoc['inversedBy']) { if ($assoc['inversedBy']) {
$class = $this->_ce[$className]; $class = $this->ce[$className];
$inverseAssoc = $class->associationMappings[$assoc['inversedBy']]; $inverseAssoc = $class->associationMappings[$assoc['inversedBy']];
if ( ! ($inverseAssoc['type'] & ClassMetadata::TO_ONE)) { if ( ! ($inverseAssoc['type'] & ClassMetadata::TO_ONE)) {
@ -127,10 +154,10 @@ class ObjectHydrator extends AbstractHydrator
parent::cleanup(); parent::cleanup();
$this->_identifierMap = $this->identifierMap =
$this->_initializedCollections = $this->initializedCollections =
$this->_existingCollections = $this->existingCollections =
$this->_resultPointers = array(); $this->resultPointers = array();
if ($eagerLoad) { if ($eagerLoad) {
$this->_em->getUnitOfWork()->triggerEagerLoads(); $this->_em->getUnitOfWork()->triggerEagerLoads();
@ -150,7 +177,7 @@ class ObjectHydrator extends AbstractHydrator
} }
// Take snapshots from all newly initialized collections // Take snapshots from all newly initialized collections
foreach ($this->_initializedCollections as $coll) { foreach ($this->initializedCollections as $coll) {
$coll->takeSnapshot(); $coll->takeSnapshot();
} }
@ -177,14 +204,14 @@ class ObjectHydrator extends AbstractHydrator
if ( ! $value instanceof PersistentCollection) { if ( ! $value instanceof PersistentCollection) {
$value = new PersistentCollection( $value = new PersistentCollection(
$this->_em, $this->_ce[$relation['targetEntity']], $value $this->_em, $this->ce[$relation['targetEntity']], $value
); );
$value->setOwner($entity, $relation); $value->setOwner($entity, $relation);
$class->reflFields[$fieldName]->setValue($entity, $value); $class->reflFields[$fieldName]->setValue($entity, $value);
$this->_uow->setOriginalEntityProperty($oid, $fieldName, $value); $this->_uow->setOriginalEntityProperty($oid, $fieldName, $value);
$this->_initializedCollections[$oid . $fieldName] = $value; $this->initializedCollections[$oid . $fieldName] = $value;
} else if ( } else if (
isset($this->_hints[Query::HINT_REFRESH]) || isset($this->_hints[Query::HINT_REFRESH]) ||
isset($this->_hints['fetched'][$parentDqlAlias][$fieldName]) && isset($this->_hints['fetched'][$parentDqlAlias][$fieldName]) &&
@ -195,10 +222,10 @@ class ObjectHydrator extends AbstractHydrator
$value->setInitialized(true); $value->setInitialized(true);
$value->unwrap()->clear(); $value->unwrap()->clear();
$this->_initializedCollections[$oid . $fieldName] = $value; $this->initializedCollections[$oid . $fieldName] = $value;
} else { } else {
// Is already PersistentCollection, and DON'T REFRESH or FETCH-JOIN! // Is already PersistentCollection, and DON'T REFRESH or FETCH-JOIN!
$this->_existingCollections[$oid . $fieldName] = $value; $this->existingCollections[$oid . $fieldName] = $value;
} }
return $value; return $value;
@ -231,13 +258,13 @@ class ObjectHydrator extends AbstractHydrator
throw HydrationException::emptyDiscriminatorValue($dqlAlias); throw HydrationException::emptyDiscriminatorValue($dqlAlias);
} }
$className = $this->_ce[$className]->discriminatorMap[$data[$discrColumn]]; $className = $this->ce[$className]->discriminatorMap[$data[$discrColumn]];
unset($data[$discrColumn]); unset($data[$discrColumn]);
} }
if (isset($this->_hints[Query::HINT_REFRESH_ENTITY]) && isset($this->_rootAliases[$dqlAlias])) { 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->registerManaged($this->ce[$className], $this->_hints[Query::HINT_REFRESH_ENTITY], $data);
} }
$this->_hints['fetchAlias'] = $dqlAlias; $this->_hints['fetchAlias'] = $dqlAlias;
@ -253,7 +280,7 @@ class ObjectHydrator extends AbstractHydrator
private function _getEntityFromIdentityMap($className, array $data) private function _getEntityFromIdentityMap($className, array $data)
{ {
// TODO: Abstract this code and UnitOfWork::createEntity() equivalent? // TODO: Abstract this code and UnitOfWork::createEntity() equivalent?
$class = $this->_ce[$className]; $class = $this->ce[$className];
/* @var $class ClassMetadata */ /* @var $class ClassMetadata */
if ($class->isIdentifierComposite) { if ($class->isIdentifierComposite) {
@ -283,11 +310,11 @@ class ObjectHydrator extends AbstractHydrator
*/ */
private function _getClassMetadata($className) private function _getClassMetadata($className)
{ {
if ( ! isset($this->_ce[$className])) { if ( ! isset($this->ce[$className])) {
$this->_ce[$className] = $this->_em->getClassMetadata($className); $this->ce[$className] = $this->_em->getClassMetadata($className);
} }
return $this->_ce[$className]; return $this->ce[$className];
} }
/** /**
@ -314,7 +341,7 @@ class ObjectHydrator extends AbstractHydrator
protected function hydrateRowData(array $row, array &$cache, array &$result) protected function hydrateRowData(array $row, array &$cache, array &$result)
{ {
// Initialize // Initialize
$id = $this->_idTemplate; // initialize the id-memory $id = $this->idTemplate; // initialize the id-memory
$nonemptyComponents = array(); $nonemptyComponents = array();
// Split the row data into chunks of class data. // Split the row data into chunks of class data.
$rowData = $this->gatherRowData($row, $cache, $id, $nonemptyComponents); $rowData = $this->gatherRowData($row, $cache, $id, $nonemptyComponents);
@ -326,7 +353,7 @@ class ObjectHydrator extends AbstractHydrator
unset($rowData['scalars']); unset($rowData['scalars']);
if (empty($rowData)) { if (empty($rowData)) {
++$this->_resultCounter; ++$this->resultCounter;
} }
} }
@ -337,7 +364,7 @@ class ObjectHydrator extends AbstractHydrator
unset($rowData['newObjects']); unset($rowData['newObjects']);
if (empty($rowData)) { if (empty($rowData)) {
++$this->_resultCounter; ++$this->resultCounter;
} }
} }
@ -360,17 +387,17 @@ class ObjectHydrator extends AbstractHydrator
} }
// Get a reference to the parent object to which the joined element belongs. // Get a reference to the parent object to which the joined element belongs.
if ($this->_rsm->isMixed && isset($this->_rootAliases[$parentAlias])) { if ($this->_rsm->isMixed && isset($this->rootAliases[$parentAlias])) {
$first = reset($this->_resultPointers); $first = reset($this->resultPointers);
$parentObject = $first[key($first)]; $parentObject = $first[key($first)];
} else if (isset($this->_resultPointers[$parentAlias])) { } else if (isset($this->resultPointers[$parentAlias])) {
$parentObject = $this->_resultPointers[$parentAlias]; $parentObject = $this->resultPointers[$parentAlias];
} else { } else {
// Parent object of relation not found, so skip it. // Parent object of relation not found, so skip it.
continue; continue;
} }
$parentClass = $this->_ce[$this->_rsm->aliasMap[$parentAlias]]; $parentClass = $this->ce[$this->_rsm->aliasMap[$parentAlias]];
$oid = spl_object_hash($parentObject); $oid = spl_object_hash($parentObject);
$relationField = $this->_rsm->relationMap[$dqlAlias]; $relationField = $this->_rsm->relationMap[$dqlAlias];
$relation = $parentClass->associationMappings[$relationField]; $relation = $parentClass->associationMappings[$relationField];
@ -382,23 +409,23 @@ class ObjectHydrator extends AbstractHydrator
// PATH A: Collection-valued association // PATH A: Collection-valued association
if (isset($nonemptyComponents[$dqlAlias])) { if (isset($nonemptyComponents[$dqlAlias])) {
$collKey = $oid . $relationField; $collKey = $oid . $relationField;
if (isset($this->_initializedCollections[$collKey])) { if (isset($this->initializedCollections[$collKey])) {
$reflFieldValue = $this->_initializedCollections[$collKey]; $reflFieldValue = $this->initializedCollections[$collKey];
} else if ( ! isset($this->_existingCollections[$collKey])) { } else if ( ! isset($this->existingCollections[$collKey])) {
$reflFieldValue = $this->_initRelatedCollection($parentObject, $parentClass, $relationField, $parentAlias); $reflFieldValue = $this->_initRelatedCollection($parentObject, $parentClass, $relationField, $parentAlias);
} }
$indexExists = isset($this->_identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]]); $indexExists = isset($this->identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]]);
$argIndex = $indexExists ? $this->_identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]] : false; $index = $indexExists ? $this->identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]] : false;
$indexIsValid = $argIndex !== false ? isset($reflFieldValue[$argIndex]) : false; $indexIsValid = $index !== false ? isset($reflFieldValue[$index]) : false;
if ( ! $indexExists || ! $indexIsValid) { if ( ! $indexExists || ! $indexIsValid) {
if (isset($this->_existingCollections[$collKey])) { if (isset($this->existingCollections[$collKey])) {
// Collection exists, only look for the element in the identity map. // Collection exists, only look for the element in the identity map.
if ($element = $this->_getEntityFromIdentityMap($entityName, $data)) { if ($element = $this->_getEntityFromIdentityMap($entityName, $data)) {
$this->_resultPointers[$dqlAlias] = $element; $this->resultPointers[$dqlAlias] = $element;
} else { } else {
unset($this->_resultPointers[$dqlAlias]); unset($this->resultPointers[$dqlAlias]);
} }
} else { } else {
$element = $this->_getEntity($data, $dqlAlias); $element = $this->_getEntity($data, $dqlAlias);
@ -406,18 +433,18 @@ class ObjectHydrator extends AbstractHydrator
if (isset($this->_rsm->indexByMap[$dqlAlias])) { if (isset($this->_rsm->indexByMap[$dqlAlias])) {
$indexValue = $row[$this->_rsm->indexByMap[$dqlAlias]]; $indexValue = $row[$this->_rsm->indexByMap[$dqlAlias]];
$reflFieldValue->hydrateSet($indexValue, $element); $reflFieldValue->hydrateSet($indexValue, $element);
$this->_identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]] = $indexValue; $this->identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]] = $indexValue;
} else { } else {
$reflFieldValue->hydrateAdd($element); $reflFieldValue->hydrateAdd($element);
$reflFieldValue->last(); $reflFieldValue->last();
$this->_identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]] = $reflFieldValue->key(); $this->identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]] = $reflFieldValue->key();
} }
// Update result pointer // Update result pointer
$this->_resultPointers[$dqlAlias] = $element; $this->resultPointers[$dqlAlias] = $element;
} }
} else { } else {
// Update result pointer // Update result pointer
$this->_resultPointers[$dqlAlias] = $reflFieldValue[$argIndex]; $this->resultPointers[$dqlAlias] = $reflFieldValue[$index];
} }
} else if ( ! $reflFieldValue) { } else if ( ! $reflFieldValue) {
$reflFieldValue = $this->_initRelatedCollection($parentObject, $parentClass, $relationField, $parentAlias); $reflFieldValue = $this->_initRelatedCollection($parentObject, $parentClass, $relationField, $parentAlias);
@ -435,7 +462,7 @@ class ObjectHydrator extends AbstractHydrator
$element = $this->_getEntity($data, $dqlAlias); $element = $this->_getEntity($data, $dqlAlias);
$reflField->setValue($parentObject, $element); $reflField->setValue($parentObject, $element);
$this->_uow->setOriginalEntityProperty($oid, $relationField, $element); $this->_uow->setOriginalEntityProperty($oid, $relationField, $element);
$targetClass = $this->_ce[$relation['targetEntity']]; $targetClass = $this->ce[$relation['targetEntity']];
if ($relation['isOwningSide']) { if ($relation['isOwningSide']) {
//TODO: Just check hints['fetched'] here? //TODO: Just check hints['fetched'] here?
@ -456,7 +483,7 @@ class ObjectHydrator extends AbstractHydrator
$this->_uow->setOriginalEntityProperty(spl_object_hash($element), $relation['mappedBy'], $parentObject); $this->_uow->setOriginalEntityProperty(spl_object_hash($element), $relation['mappedBy'], $parentObject);
} }
// Update result pointer // Update result pointer
$this->_resultPointers[$dqlAlias] = $element; $this->resultPointers[$dqlAlias] = $element;
} else { } else {
$this->_uow->setOriginalEntityProperty($oid, $relationField, null); $this->_uow->setOriginalEntityProperty($oid, $relationField, null);
$reflField->setValue($parentObject, null); $reflField->setValue($parentObject, null);
@ -464,12 +491,12 @@ class ObjectHydrator extends AbstractHydrator
// else leave $reflFieldValue null for single-valued associations // else leave $reflFieldValue null for single-valued associations
} else { } else {
// Update result pointer // Update result pointer
$this->_resultPointers[$dqlAlias] = $reflFieldValue; $this->resultPointers[$dqlAlias] = $reflFieldValue;
} }
} }
} else { } else {
// PATH C: Its a root result element // PATH C: Its a root result element
$this->_rootAliases[$dqlAlias] = true; // Mark as root alias $this->rootAliases[$dqlAlias] = true; // Mark as root alias
$entityKey = $this->_rsm->entityMappings[$dqlAlias] ?: 0; $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 this row has a NULL value for the root result id then make it a null result.
@ -479,13 +506,13 @@ class ObjectHydrator extends AbstractHydrator
} else { } else {
$result[] = null; $result[] = null;
} }
$resultKey = $this->_resultCounter; $resultKey = $this->resultCounter;
++$this->_resultCounter; ++$this->resultCounter;
continue; continue;
} }
// check for existing result from the iterations before // check for existing result from the iterations before
if ( ! isset($this->_identifierMap[$dqlAlias][$id[$dqlAlias]])) { if ( ! isset($this->identifierMap[$dqlAlias][$id[$dqlAlias]])) {
$element = $this->_getEntity($rowData[$dqlAlias], $dqlAlias); $element = $this->_getEntity($rowData[$dqlAlias], $dqlAlias);
if ($this->_rsm->isMixed) { if ($this->_rsm->isMixed) {
$element = array($entityKey => $element); $element = array($entityKey => $element);
@ -500,8 +527,8 @@ class ObjectHydrator extends AbstractHydrator
$result[$resultKey] = $element; $result[$resultKey] = $element;
} else { } else {
$resultKey = $this->_resultCounter; $resultKey = $this->resultCounter;
++$this->_resultCounter; ++$this->resultCounter;
if (isset($this->_hints['collection'])) { if (isset($this->_hints['collection'])) {
$this->_hints['collection']->hydrateAdd($element); $this->_hints['collection']->hydrateAdd($element);
@ -510,16 +537,16 @@ class ObjectHydrator extends AbstractHydrator
$result[] = $element; $result[] = $element;
} }
$this->_identifierMap[$dqlAlias][$id[$dqlAlias]] = $resultKey; $this->identifierMap[$dqlAlias][$id[$dqlAlias]] = $resultKey;
// Update result pointer // Update result pointer
$this->_resultPointers[$dqlAlias] = $element; $this->resultPointers[$dqlAlias] = $element;
} else { } else {
// Update result pointer // Update result pointer
$argIndex = $this->_identifierMap[$dqlAlias][$id[$dqlAlias]]; $index = $this->identifierMap[$dqlAlias][$id[$dqlAlias]];
$this->_resultPointers[$dqlAlias] = $result[$argIndex]; $this->resultPointers[$dqlAlias] = $result[$index];
$resultKey = $argIndex; $resultKey = $index;
/*if ($this->_rsm->isMixed) { /*if ($this->_rsm->isMixed) {
$result[] = $result[$index]; $result[] = $result[$index];
++$this->_resultCounter; ++$this->_resultCounter;
@ -534,7 +561,7 @@ class ObjectHydrator extends AbstractHydrator
if (isset($this->_rsm->indexByMap['scalars'])) { if (isset($this->_rsm->indexByMap['scalars'])) {
$resultKey = $row[$this->_rsm->indexByMap['scalars']]; $resultKey = $row[$this->_rsm->indexByMap['scalars']];
} else { } else {
$resultKey = $this->_resultCounter - 1; $resultKey = $this->resultCounter - 1;
} }
} }
@ -546,7 +573,7 @@ class ObjectHydrator extends AbstractHydrator
// Append new object to mixed result sets // Append new object to mixed result sets
if (isset($newObjects)) { if (isset($newObjects)) {
if ( ! isset($resultKey) ) { if ( ! isset($resultKey) ) {
$resultKey = $this->_resultCounter - 1; $resultKey = $this->resultCounter - 1;
} }
$count = count($newObjects); $count = count($newObjects);
@ -575,11 +602,11 @@ class ObjectHydrator extends AbstractHydrator
{ {
parent::onClear($eventArgs); parent::onClear($eventArgs);
$aliases = array_keys($this->_identifierMap); $aliases = array_keys($this->identifierMap);
$this->_identifierMap = array(); $this->identifierMap = array();
foreach ($aliases as $alias) { foreach ($aliases as $alias) {
$this->_identifierMap[$alias] = array(); $this->identifierMap[$alias] = array();
} }
} }
} }