From 4d146d321f4c6cd56ec4f34128ecc1c7d9152d98 Mon Sep 17 00:00:00 2001 From: romanb Date: Mon, 20 Jul 2009 10:52:07 +0000 Subject: [PATCH] [2.0] Small hydration cleanups and improvements. --- .../Internal/Hydration/AbstractHydrator.php | 37 ++++--------------- .../ORM/Internal/Hydration/ArrayHydrator.php | 16 +++++--- .../ORM/Internal/Hydration/ObjectHydrator.php | 23 +++++++----- 3 files changed, 32 insertions(+), 44 deletions(-) diff --git a/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php b/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php index a2a1fb0c7..956de0425 100644 --- a/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php +++ b/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php @@ -178,15 +178,15 @@ abstract class AbstractHydrator foreach ($data as $key => $value) { // Parse each column name only once. Cache the results. if ( ! isset($cache[$key])) { - if (isset($this->_rsm->ignoredColumns[$key])) { - $cache[$key] = false; - } else if (isset($this->_rsm->scalarMappings[$key])) { + if (isset($this->_rsm->scalarMappings[$key])) { $cache[$key]['fieldName'] = $this->_rsm->scalarMappings[$key]; $cache[$key]['isScalar'] = true; } else if (isset($this->_rsm->fieldMappings[$key])) { $classMetadata = $this->_em->getClassMetadata($this->_rsm->getOwningClass($key)); $fieldName = $this->_rsm->fieldMappings[$key]; - $classMetadata = $this->_lookupDeclaringClass($classMetadata, $fieldName); + if ( ! isset($classMetadata->reflFields[$fieldName])) { + $classMetadata = $this->_lookupDeclaringClass($classMetadata, $fieldName); + } $cache[$key]['fieldName'] = $fieldName; $cache[$key]['isScalar'] = false; $cache[$key]['type'] = Type::getType($classMetadata->fieldMappings[$fieldName]['type']); @@ -222,11 +222,6 @@ abstract class AbstractHydrator if ( ! isset($nonemptyComponents[$dqlAlias]) && $value !== null) { $nonemptyComponents[$dqlAlias] = true; } - - /* TODO: Consider this instead of the above 4 lines. */ - /*if ($value !== null) { - $rowData[$dqlAlias][$fieldName] = $cache[$key]['type']->convertToPHPValue($value, $this->_platform); - }*/ } return $rowData; @@ -249,16 +244,15 @@ abstract class AbstractHydrator foreach ($data as $key => $value) { // Parse each column name only once. Cache the results. if ( ! isset($cache[$key])) { - if (isset($this->_rsm->ignoredColumns[$key])) { - $cache[$key] = false; - continue; - } else if (isset($this->_rsm->scalarMappings[$key])) { + if (isset($this->_rsm->scalarMappings[$key])) { $cache[$key]['fieldName'] = $this->_rsm->scalarMappings[$key]; $cache[$key]['isScalar'] = true; } else { $classMetadata = $this->_em->getClassMetadata($this->_rsm->getOwningClass($key)); $fieldName = $this->_rsm->fieldMappings[$key]; - $classMetadata = $this->_lookupDeclaringClass($classMetadata, $fieldName); + if ( ! isset($classMetadata->reflFields[$fieldName])) { + $classMetadata = $this->_lookupDeclaringClass($classMetadata, $fieldName); + } $cache[$key]['fieldName'] = $fieldName; $cache[$key]['isScalar'] = false; $cache[$key]['type'] = Type::getType($classMetadata->getTypeOfField($fieldName)); @@ -279,17 +273,6 @@ abstract class AbstractHydrator return $rowData; } - /** - * Gets the custom field used for indexing for the specified DQL alias. - * - * @return string The field name of the field used for indexing or NULL - * if the component does not use any custom field indices. - */ - protected function _getCustomIndexField($alias) - { - return isset($this->_rsm->indexByMap[$alias]) ? $this->_rsm->indexByMap[$alias] : null; - } - /** * Looks up the field name for a (lowercased) column name. * @@ -310,10 +293,6 @@ abstract class AbstractHydrator */ private function _lookupDeclaringClass($class, $fieldName) { - if (isset($class->reflFields[$fieldName])) { - return $class; - } - foreach ($class->subClasses as $subClass) { $subClassMetadata = $this->_em->getClassMetadata($subClass); if ($subClassMetadata->hasField($fieldName)) { diff --git a/lib/Doctrine/ORM/Internal/Hydration/ArrayHydrator.php b/lib/Doctrine/ORM/Internal/Hydration/ArrayHydrator.php index 6b5396f73..ce581edad 100644 --- a/lib/Doctrine/ORM/Internal/Hydration/ArrayHydrator.php +++ b/lib/Doctrine/ORM/Internal/Hydration/ArrayHydrator.php @@ -24,9 +24,11 @@ namespace Doctrine\ORM\Internal\Hydration; use Doctrine\DBAL\Connection; /** - * Description of ArrayHydrator + * The ArrayHydrator produces a nested array "graph" that is often (not always) + * interchangeable with the corresponding object graph for read-only access. * - * @author robo + * @author Roman Borschel + * @since 1.0 */ class ArrayHydrator extends AbstractHydrator { @@ -116,7 +118,8 @@ class ArrayHydrator extends AbstractHydrator $indexIsValid = $index !== false ? isset($baseElement[$relationAlias][$index]) : false; if ( ! $indexExists || ! $indexIsValid) { $element = $data; - if ($field = $this->_getCustomIndexField($dqlAlias)) { + if (isset($this->_rsm->indexByMap[$dqlAlias])) { + $field = $this->_rsm->indexByMap[$dqlAlias]; $baseElement[$relationAlias][$element[$field]] = $element; } else { $baseElement[$relationAlias][] = $element; @@ -144,12 +147,15 @@ class ArrayHydrator extends AbstractHydrator } } else { + // It's a root result element + $this->_rootAliases[$dqlAlias] = true; // Mark as root - // 2) Hydrate the data of the root entity from the current row + // Check for an existing element if ($this->_isSimpleQuery || ! isset($this->_identifierMap[$dqlAlias][$id[$dqlAlias]])) { $element = $rowData[$dqlAlias]; - if ($field = $this->_getCustomIndexField($dqlAlias)) { + if (isset($this->_rsm->indexByMap[$dqlAlias])) { + $field = $this->_rsm->indexByMap[$dqlAlias]; if ($this->_rsm->isMixed) { $result[] = array($element[$field] => $element); ++$this->_resultCounter; diff --git a/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php b/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php index 3f304ad4a..728c6a852 100644 --- a/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php +++ b/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php @@ -168,10 +168,10 @@ class ObjectHydrator extends AbstractHydrator } /** + * Initializes a related collection. * - * @param object $entity - * @param string $name - * @todo Consider inlining this method. + * @param object $entity The entity to which the collection belongs. + * @param string $name The name of the field on the entity that holds the collection. */ private function initRelatedCollection($entity, $name) { @@ -179,16 +179,16 @@ class ObjectHydrator extends AbstractHydrator $classMetadata = $this->_ce[get_class($entity)]; $relation = $classMetadata->associationMappings[$name]; - $relatedClass = $this->_em->getClassMetadata($relation->targetEntityName); - $coll = new PersistentCollection($this->_em, $relatedClass); + if ( ! isset($this->_ce[$relation->targetEntityName])) { + $this->_ce[$relation->targetEntityName] = $this->_em->getClassMetadata($relation->targetEntityName); + } + $coll = new PersistentCollection($this->_em, $this->_ce[$relation->targetEntityName]); $this->_collections[] = $coll; $coll->setOwner($entity, $relation); $classMetadata->reflFields[$name]->setValue($entity, $coll); $this->_uow->setOriginalEntityProperty($oid, $name, $coll); $this->_initializedRelations[$oid][$name] = true; - - return $coll; } /** @@ -197,7 +197,7 @@ class ObjectHydrator extends AbstractHydrator * @param $assocField * @param $indexField * @return - * @todo Consider inlining this method. + * @todo Inline this method. */ private function isIndexKeyInUse($entity, $assocField, $indexField) { @@ -371,7 +371,8 @@ class ObjectHydrator extends AbstractHydrator } } - if ($field = $this->_getCustomIndexField($dqlAlias)) { + if (isset($this->_rsm->indexByMap[$dqlAlias])) { + $field = $this->_rsm->indexByMap[$dqlAlias]; $indexValue = $this->_ce[$entityName] ->reflFields[$field] ->getValue($element); @@ -385,6 +386,7 @@ class ObjectHydrator extends AbstractHydrator ->getValue($baseElement) ->hydrateAdd($element); } + $this->_identifierMap[$path][$id[$parent]][$id[$dqlAlias]] = $this->getLastKey( $this->_ce[$parentClass] ->reflFields[$relationAlias] @@ -416,7 +418,8 @@ class ObjectHydrator extends AbstractHydrator if ($this->_isSimpleQuery || ! isset($this->_identifierMap[$dqlAlias][$id[$dqlAlias]])) { $element = $this->getEntity($rowData[$dqlAlias], $dqlAlias); - if ($field = $this->_getCustomIndexField($dqlAlias)) { + if (isset($this->_rsm->indexByMap[$dqlAlias])) { + $field = $this->_rsm->indexByMap[$dqlAlias]; if ($this->_rsm->isMixed) { $result[] = array( $this->_ce[$entityName]