1
0
Fork 0
mirror of synced 2025-04-01 12:26:11 +03:00

Readability update to hydrators.

This commit is contained in:
Guilherme Blanco 2014-04-29 05:05:38 +00:00
parent 9cd16ec56a
commit 48172f3a53
3 changed files with 43 additions and 84 deletions

View file

@ -257,9 +257,7 @@ abstract class AbstractHydrator
$rowData = array('data' => array());
foreach ($data as $key => $value) {
$cacheKeyInfo = $this->hydrateColumnInfo($key);
if ( ! $cacheKeyInfo) {
if (($cacheKeyInfo = $this->hydrateColumnInfo($key)) === null) {
continue;
}
@ -285,25 +283,7 @@ abstract class AbstractHydrator
$rowData['scalars'][$fieldName] = $value;
break;
case (isset($cacheKeyInfo['isMetaColumn'])):
$dqlAlias = $cacheKeyInfo['dqlAlias'];
$type = $cacheKeyInfo['type'];
// Avoid double setting or null assignment
if (isset($rowData['data'][$dqlAlias][$fieldName]) || $value === null) {
break;
}
if ($cacheKeyInfo['isIdentifier']) {
$id[$dqlAlias] .= '|' . $value;
$nonemptyComponents[$dqlAlias] = true;
}
$rowData['data'][$dqlAlias][$fieldName] = $type
? $type->convertToPHPValue($value, $this->_platform)
: $value;
break;
//case (isset($cacheKeyInfo['isMetaColumn'])):
default:
$dqlAlias = $cacheKeyInfo['dqlAlias'];
$type = $cacheKeyInfo['type'];
@ -311,21 +291,18 @@ abstract class AbstractHydrator
// in an inheritance hierarchy the same field could be defined several times.
// We overwrite this value so long we don't have a non-null value, that value we keep.
// Per definition it cannot be that a field is defined several times and has several values.
if (isset($rowData['data'][$dqlAlias][$fieldName]) && $value === null) {
if (isset($rowData['data'][$dqlAlias][$fieldName])) {
break;
}
if ($cacheKeyInfo['isIdentifier']) {
$rowData['data'][$dqlAlias][$fieldName] = $type
? $type->convertToPHPValue($value, $this->_platform)
: $value;
if ($cacheKeyInfo['isIdentifier'] && $value !== null) {
$id[$dqlAlias] .= '|' . $value;
}
$value = $type->convertToPHPValue($value, $this->_platform);
if ( ! isset($nonemptyComponents[$dqlAlias]) && $value !== null) {
$nonemptyComponents[$dqlAlias] = true;
}
$rowData['data'][$dqlAlias][$fieldName] = $value;
break;
}
}
@ -350,41 +327,24 @@ abstract class AbstractHydrator
$rowData = array();
foreach ($data as $key => $value) {
$cacheKeyInfo = $this->hydrateColumnInfo($key);
if ( ! $cacheKeyInfo) {
if (($cacheKeyInfo = $this->hydrateColumnInfo($key)) === null) {
continue;
}
$fieldName = $cacheKeyInfo['fieldName'];
switch (true) {
case (isset($cacheKeyInfo['isScalar'])):
// WARNING: BC break! We know this is the desired behavior to type convert values, but this
// erroneous behavior exists since 2.0 and we're forced to keep compatibility. For 3.0 release,
// uncomment these 2 lines of code.
//$type = $cacheKeyInfo['type'];
//$value = $type->convertToPHPValue($value, $this->_platform);
$rowData[$fieldName] = $value;
break;
case (isset($cacheKeyInfo['isMetaColumn'])):
$dqlAlias = $cacheKeyInfo['dqlAlias'];
$type = $cacheKeyInfo['type'];
$rowData[$dqlAlias . '_' . $fieldName] = $type
? $type->convertToPHPValue($value, $this->_platform)
: $value;
break;
default:
$dqlAlias = $cacheKeyInfo['dqlAlias'];
$type = $cacheKeyInfo['type'];
$value = $type->convertToPHPValue($value, $this->_platform);
$rowData[$dqlAlias . '_' . $fieldName] = $value;
// WARNING: BC break! We know this is the desired behavior to type convert values, but this
// erroneous behavior exists since 2.0 and we're forced to keep compatibility.
if ( ! isset($cacheKeyInfo['isScalar'])) {
$dqlAlias = $cacheKeyInfo['dqlAlias'];
$type = $cacheKeyInfo['type'];
$fieldName = $dqlAlias . '_' . $fieldName;
$value = $type
? $type->convertToPHPValue($value, $this->_platform)
: $value;
}
$rowData[$fieldName] = $value;
}
return $rowData;
@ -406,12 +366,12 @@ abstract class AbstractHydrator
switch (true) {
// NOTE: Most of the times it's a field mapping, so keep it first!!!
case (isset($this->_rsm->fieldMappings[$key])):
$fieldName = $this->_rsm->fieldMappings[$key];
$classMetadata = $this->getClassMetadata($this->_rsm->declaringClasses[$key]);
$fieldName = $this->_rsm->fieldMappings[$key];
$fieldMapping = $classMetadata->fieldMappings[$fieldName];
return $this->_cache[$key] = array(
'isIdentifier' => $classMetadata->isIdentifier($fieldName),
'isIdentifier' => in_array($fieldName, $classMetadata->identifier),
'fieldName' => $fieldName,
'type' => Type::getType($fieldMapping['type']),
'dqlAlias' => $this->_rsm->columnOwnerMap[$key],
@ -499,11 +459,11 @@ abstract class AbstractHydrator
: $data[$fieldName];
}
} else {
$identifier = $class->identifier[0];
$id = array(
$identifier => isset($class->associationMappings[$identifier])
? $data[$class->associationMappings[$identifier]['joinColumns'][0]['name']]
: $data[$identifier]
$fieldName = $class->identifier[0];
$id = array(
$fieldName => isset($class->associationMappings[$fieldName])
? $data[$class->associationMappings[$fieldName]['joinColumns'][0]['name']]
: $data[$fieldName]
);
}

View file

@ -137,11 +137,11 @@ class ArrayHydrator extends AbstractHydrator
if ( ! ($relation['type'] & ClassMetadata::TO_ONE)) {
$oneToOne = false;
if (isset($nonemptyComponents[$dqlAlias])) {
if ( ! isset($baseElement[$relationAlias])) {
$baseElement[$relationAlias] = array();
}
if ( ! isset($baseElement[$relationAlias])) {
$baseElement[$relationAlias] = array();
}
if (isset($nonemptyComponents[$dqlAlias])) {
$indexExists = isset($this->_identifierMap[$path][$id[$parent]][$id[$dqlAlias]]);
$index = $indexExists ? $this->_identifierMap[$path][$id[$parent]][$id[$dqlAlias]] : false;
$indexIsValid = $index !== false ? isset($baseElement[$relationAlias][$index]) : false;
@ -159,8 +159,6 @@ class ArrayHydrator extends AbstractHydrator
$this->_identifierMap[$path][$id[$parent]][$id[$dqlAlias]] = key($baseElement[$relationAlias]);
}
} else if ( ! isset($baseElement[$relationAlias])) {
$baseElement[$relationAlias] = array();
}
} else {
$oneToOne = true;

View file

@ -232,12 +232,13 @@ class ObjectHydrator extends AbstractHydrator
$className = $this->_rsm->aliasMap[$dqlAlias];
if (isset($this->_rsm->discriminatorColumns[$dqlAlias])) {
$fieldName = $this->_rsm->discriminatorColumns[$dqlAlias];
if ( ! isset($this->_rsm->metaMappings[$this->_rsm->discriminatorColumns[$dqlAlias]])) {
throw HydrationException::missingDiscriminatorMetaMappingColumn($className, $this->_rsm->discriminatorColumns[$dqlAlias], $dqlAlias);
if ( ! isset($this->_rsm->metaMappings[$fieldName])) {
throw HydrationException::missingDiscriminatorMetaMappingColumn($className, $fieldName, $dqlAlias);
}
$discrColumn = $this->_rsm->metaMappings[$this->_rsm->discriminatorColumns[$dqlAlias]];
$discrColumn = $this->_rsm->metaMappings[$fieldName];
if ( ! isset($data[$discrColumn])) {
throw HydrationException::missingDiscriminatorColumn($className, $discrColumn, $dqlAlias);
@ -281,19 +282,19 @@ class ObjectHydrator extends AbstractHydrator
/* @var $class ClassMetadata */
if ($class->isIdentifierComposite) {
$idHash = '';
foreach ($class->identifier as $fieldName) {
if (isset($class->associationMappings[$fieldName])) {
$idHash .= $data[$class->associationMappings[$fieldName]['joinColumns'][0]['name']] . ' ';
} else {
$idHash .= $data[$fieldName] . ' ';
}
$idHash .= ' ' . (isset($class->associationMappings[$fieldName])
? $data[$class->associationMappings[$fieldName]['joinColumns'][0]['name']]
: $data[$fieldName]);
}
return $this->_uow->tryGetByIdHash(rtrim($idHash), $class->rootEntityName);
return $this->_uow->tryGetByIdHash(ltrim($idHash), $class->rootEntityName);
} else if (isset($class->associationMappings[$class->identifier[0]])) {
return $this->_uow->tryGetByIdHash($data[$class->associationMappings[$class->identifier[0]]['joinColumns'][0]['name']], $class->rootEntityName);
} else {
return $this->_uow->tryGetByIdHash($data[$class->identifier[0]], $class->rootEntityName);
}
return $this->_uow->tryGetByIdHash($data[$class->identifier[0]], $class->rootEntityName);
}
/**