1
0
mirror of synced 2025-01-18 22:41:43 +03:00

[2.0] Small hydration cleanups and improvements.

This commit is contained in:
romanb 2009-07-20 10:52:07 +00:00
parent e197348734
commit 4d146d321f
3 changed files with 32 additions and 44 deletions

View File

@ -178,15 +178,15 @@ abstract class AbstractHydrator
foreach ($data as $key => $value) { foreach ($data as $key => $value) {
// Parse each column name only once. Cache the results. // Parse each column name only once. Cache the results.
if ( ! isset($cache[$key])) { if ( ! isset($cache[$key])) {
if (isset($this->_rsm->ignoredColumns[$key])) { if (isset($this->_rsm->scalarMappings[$key])) {
$cache[$key] = false;
} else if (isset($this->_rsm->scalarMappings[$key])) {
$cache[$key]['fieldName'] = $this->_rsm->scalarMappings[$key]; $cache[$key]['fieldName'] = $this->_rsm->scalarMappings[$key];
$cache[$key]['isScalar'] = true; $cache[$key]['isScalar'] = true;
} else if (isset($this->_rsm->fieldMappings[$key])) { } else if (isset($this->_rsm->fieldMappings[$key])) {
$classMetadata = $this->_em->getClassMetadata($this->_rsm->getOwningClass($key)); $classMetadata = $this->_em->getClassMetadata($this->_rsm->getOwningClass($key));
$fieldName = $this->_rsm->fieldMappings[$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]['fieldName'] = $fieldName;
$cache[$key]['isScalar'] = false; $cache[$key]['isScalar'] = false;
$cache[$key]['type'] = Type::getType($classMetadata->fieldMappings[$fieldName]['type']); $cache[$key]['type'] = Type::getType($classMetadata->fieldMappings[$fieldName]['type']);
@ -222,11 +222,6 @@ abstract class AbstractHydrator
if ( ! isset($nonemptyComponents[$dqlAlias]) && $value !== null) { if ( ! isset($nonemptyComponents[$dqlAlias]) && $value !== null) {
$nonemptyComponents[$dqlAlias] = true; $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; return $rowData;
@ -249,16 +244,15 @@ abstract class AbstractHydrator
foreach ($data as $key => $value) { foreach ($data as $key => $value) {
// Parse each column name only once. Cache the results. // Parse each column name only once. Cache the results.
if ( ! isset($cache[$key])) { if ( ! isset($cache[$key])) {
if (isset($this->_rsm->ignoredColumns[$key])) { if (isset($this->_rsm->scalarMappings[$key])) {
$cache[$key] = false;
continue;
} else if (isset($this->_rsm->scalarMappings[$key])) {
$cache[$key]['fieldName'] = $this->_rsm->scalarMappings[$key]; $cache[$key]['fieldName'] = $this->_rsm->scalarMappings[$key];
$cache[$key]['isScalar'] = true; $cache[$key]['isScalar'] = true;
} else { } else {
$classMetadata = $this->_em->getClassMetadata($this->_rsm->getOwningClass($key)); $classMetadata = $this->_em->getClassMetadata($this->_rsm->getOwningClass($key));
$fieldName = $this->_rsm->fieldMappings[$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]['fieldName'] = $fieldName;
$cache[$key]['isScalar'] = false; $cache[$key]['isScalar'] = false;
$cache[$key]['type'] = Type::getType($classMetadata->getTypeOfField($fieldName)); $cache[$key]['type'] = Type::getType($classMetadata->getTypeOfField($fieldName));
@ -279,17 +273,6 @@ abstract class AbstractHydrator
return $rowData; 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. * Looks up the field name for a (lowercased) column name.
* *
@ -310,10 +293,6 @@ abstract class AbstractHydrator
*/ */
private function _lookupDeclaringClass($class, $fieldName) private function _lookupDeclaringClass($class, $fieldName)
{ {
if (isset($class->reflFields[$fieldName])) {
return $class;
}
foreach ($class->subClasses as $subClass) { foreach ($class->subClasses as $subClass) {
$subClassMetadata = $this->_em->getClassMetadata($subClass); $subClassMetadata = $this->_em->getClassMetadata($subClass);
if ($subClassMetadata->hasField($fieldName)) { if ($subClassMetadata->hasField($fieldName)) {

View File

@ -24,9 +24,11 @@ namespace Doctrine\ORM\Internal\Hydration;
use Doctrine\DBAL\Connection; 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 <roman@code-factory.org>
* @since 1.0
*/ */
class ArrayHydrator extends AbstractHydrator class ArrayHydrator extends AbstractHydrator
{ {
@ -116,7 +118,8 @@ class ArrayHydrator extends AbstractHydrator
$indexIsValid = $index !== false ? isset($baseElement[$relationAlias][$index]) : false; $indexIsValid = $index !== false ? isset($baseElement[$relationAlias][$index]) : false;
if ( ! $indexExists || ! $indexIsValid) { if ( ! $indexExists || ! $indexIsValid) {
$element = $data; $element = $data;
if ($field = $this->_getCustomIndexField($dqlAlias)) { if (isset($this->_rsm->indexByMap[$dqlAlias])) {
$field = $this->_rsm->indexByMap[$dqlAlias];
$baseElement[$relationAlias][$element[$field]] = $element; $baseElement[$relationAlias][$element[$field]] = $element;
} else { } else {
$baseElement[$relationAlias][] = $element; $baseElement[$relationAlias][] = $element;
@ -144,12 +147,15 @@ class ArrayHydrator extends AbstractHydrator
} }
} else { } else {
// It's a root result element
$this->_rootAliases[$dqlAlias] = true; // Mark as root $this->_rootAliases[$dqlAlias] = true; // Mark as root
// 2) Hydrate the data of the root entity from the current row
// Check for an existing element // Check for an existing element
if ($this->_isSimpleQuery || ! isset($this->_identifierMap[$dqlAlias][$id[$dqlAlias]])) { if ($this->_isSimpleQuery || ! isset($this->_identifierMap[$dqlAlias][$id[$dqlAlias]])) {
$element = $rowData[$dqlAlias]; $element = $rowData[$dqlAlias];
if ($field = $this->_getCustomIndexField($dqlAlias)) { if (isset($this->_rsm->indexByMap[$dqlAlias])) {
$field = $this->_rsm->indexByMap[$dqlAlias];
if ($this->_rsm->isMixed) { if ($this->_rsm->isMixed) {
$result[] = array($element[$field] => $element); $result[] = array($element[$field] => $element);
++$this->_resultCounter; ++$this->_resultCounter;

View File

@ -168,10 +168,10 @@ class ObjectHydrator extends AbstractHydrator
} }
/** /**
* Initializes a related collection.
* *
* @param object $entity * @param object $entity The entity to which the collection belongs.
* @param string $name * @param string $name The name of the field on the entity that holds the collection.
* @todo Consider inlining this method.
*/ */
private function initRelatedCollection($entity, $name) private function initRelatedCollection($entity, $name)
{ {
@ -179,16 +179,16 @@ class ObjectHydrator extends AbstractHydrator
$classMetadata = $this->_ce[get_class($entity)]; $classMetadata = $this->_ce[get_class($entity)];
$relation = $classMetadata->associationMappings[$name]; $relation = $classMetadata->associationMappings[$name];
$relatedClass = $this->_em->getClassMetadata($relation->targetEntityName); if ( ! isset($this->_ce[$relation->targetEntityName])) {
$coll = new PersistentCollection($this->_em, $relatedClass); $this->_ce[$relation->targetEntityName] = $this->_em->getClassMetadata($relation->targetEntityName);
}
$coll = new PersistentCollection($this->_em, $this->_ce[$relation->targetEntityName]);
$this->_collections[] = $coll; $this->_collections[] = $coll;
$coll->setOwner($entity, $relation); $coll->setOwner($entity, $relation);
$classMetadata->reflFields[$name]->setValue($entity, $coll); $classMetadata->reflFields[$name]->setValue($entity, $coll);
$this->_uow->setOriginalEntityProperty($oid, $name, $coll); $this->_uow->setOriginalEntityProperty($oid, $name, $coll);
$this->_initializedRelations[$oid][$name] = true; $this->_initializedRelations[$oid][$name] = true;
return $coll;
} }
/** /**
@ -197,7 +197,7 @@ class ObjectHydrator extends AbstractHydrator
* @param <type> $assocField * @param <type> $assocField
* @param <type> $indexField * @param <type> $indexField
* @return <type> * @return <type>
* @todo Consider inlining this method. * @todo Inline this method.
*/ */
private function isIndexKeyInUse($entity, $assocField, $indexField) 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] $indexValue = $this->_ce[$entityName]
->reflFields[$field] ->reflFields[$field]
->getValue($element); ->getValue($element);
@ -385,6 +386,7 @@ class ObjectHydrator extends AbstractHydrator
->getValue($baseElement) ->getValue($baseElement)
->hydrateAdd($element); ->hydrateAdd($element);
} }
$this->_identifierMap[$path][$id[$parent]][$id[$dqlAlias]] = $this->getLastKey( $this->_identifierMap[$path][$id[$parent]][$id[$dqlAlias]] = $this->getLastKey(
$this->_ce[$parentClass] $this->_ce[$parentClass]
->reflFields[$relationAlias] ->reflFields[$relationAlias]
@ -416,7 +418,8 @@ class ObjectHydrator extends AbstractHydrator
if ($this->_isSimpleQuery || ! isset($this->_identifierMap[$dqlAlias][$id[$dqlAlias]])) { if ($this->_isSimpleQuery || ! isset($this->_identifierMap[$dqlAlias][$id[$dqlAlias]])) {
$element = $this->getEntity($rowData[$dqlAlias], $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) { if ($this->_rsm->isMixed) {
$result[] = array( $result[] = array(
$this->_ce[$entityName] $this->_ce[$entityName]