1
0
mirror of synced 2025-01-18 14:31:40 +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) {
// 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)) {

View File

@ -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 <roman@code-factory.org>
* @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;

View File

@ -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 <type> $assocField
* @param <type> $indexField
* @return <type>
* @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]