From a8478d5766e2cc4185612af680b7f6bcd83af61e Mon Sep 17 00:00:00 2001 From: Guilherme Blanco Date: Mon, 19 Dec 2011 01:39:48 -0500 Subject: [PATCH] Fixed issue with fetched association not being considered during changeSet calculation. Fixes DDC-1545. --- lib/Doctrine/ORM/Query/SqlWalker.php | 6 +---- lib/Doctrine/ORM/UnitOfWork.php | 27 +++++++++++-------- .../Doctrine/Tests/Models/CMS/CmsArticle.php | 2 +- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index 0bc437a98..753669f92 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -978,17 +978,13 @@ class SqlWalker implements TreeWalker */ public function walkCoalesceExpression($coalesceExpression) { - $sql = 'COALESCE('; - $scalarExpressions = array(); foreach ($coalesceExpression->scalarExpressions as $scalarExpression) { $scalarExpressions[] = $this->walkSimpleArithmeticExpression($scalarExpression); } - $sql .= implode(', ', $scalarExpressions) . ')'; - - return $sql; + return 'COALESCE(' . implode(', ', $scalarExpressions) . ')'; } /** diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index a026d001a..1db6caa0c 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -705,7 +705,6 @@ class UnitOfWork implements PropertyChangedListener foreach ($unwrappedValue as $key => $entry) { $state = $this->getEntityState($entry, self::STATE_NEW); - $oid = spl_object_hash($entry); switch ($state) { case self::STATE_NEW: @@ -2293,13 +2292,14 @@ class UnitOfWork implements PropertyChangedListener $id = array($class->identifier[0] => $idHash); } + $overrideLocalValues = true; + if (isset($this->identityMap[$class->rootEntityName][$idHash])) { $entity = $this->identityMap[$class->rootEntityName][$idHash]; $oid = spl_object_hash($entity); if ($entity instanceof Proxy && ! $entity->__isInitialized__) { $entity->__isInitialized__ = true; - $overrideLocalValues = true; if ($entity instanceof NotifyPropertyChanged) { $entity->addPropertyChangedListener($this); @@ -2308,7 +2308,7 @@ class UnitOfWork implements PropertyChangedListener $overrideLocalValues = isset($hints[Query::HINT_REFRESH]); // If only a specific entity is set to refresh, check that it's the one - if(isset($hints[Query::HINT_REFRESH_ENTITY])) { + if (isset($hints[Query::HINT_REFRESH_ENTITY])) { $overrideLocalValues = $hints[Query::HINT_REFRESH_ENTITY] === $entity; // inject ObjectManager into just loaded proxies. @@ -2333,8 +2333,6 @@ class UnitOfWork implements PropertyChangedListener if ($entity instanceof NotifyPropertyChanged) { $entity->addPropertyChangedListener($this); } - - $overrideLocalValues = true; } if ( ! $overrideLocalValues) { @@ -2362,6 +2360,10 @@ class UnitOfWork implements PropertyChangedListener foreach ($class->associationMappings as $field => $assoc) { // Check if the association is not among the fetch-joined associations already. if (isset($hints['fetchAlias']) && isset($hints['fetched'][$hints['fetchAlias']][$field])) { + // DDC-1545: Fetched associations must have original entity data set. + // Define NULL value right now, since next iteration may fill it with actual value. + $this->originalEntityData[$oid][$field] = null; + continue; } @@ -2382,12 +2384,15 @@ class UnitOfWork implements PropertyChangedListener foreach ($assoc['targetToSourceKeyColumns'] as $targetColumn => $srcColumn) { $joinColumnValue = isset($data[$srcColumn]) ? $data[$srcColumn] : null; - if ($joinColumnValue !== null) { - if ($targetClass->containsForeignIdentifier) { - $associatedId[$targetClass->getFieldForColumn($targetColumn)] = $joinColumnValue; - } else { - $associatedId[$targetClass->fieldNames[$targetColumn]] = $joinColumnValue; - } + // Skip is join column value is null + if ($joinColumnValue === null) { + continue; + } + + if ($targetClass->containsForeignIdentifier) { + $associatedId[$targetClass->getFieldForColumn($targetColumn)] = $joinColumnValue; + } else { + $associatedId[$targetClass->fieldNames[$targetColumn]] = $joinColumnValue; } } diff --git a/tests/Doctrine/Tests/Models/CMS/CmsArticle.php b/tests/Doctrine/Tests/Models/CMS/CmsArticle.php index 5cc516735..266cbc662 100644 --- a/tests/Doctrine/Tests/Models/CMS/CmsArticle.php +++ b/tests/Doctrine/Tests/Models/CMS/CmsArticle.php @@ -36,7 +36,7 @@ class CmsArticle * @Version @column(type="integer") */ public $version; - + public function setAuthor(CmsUser $author) { $this->user = $author; }