diff --git a/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php b/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php index cb33a56a6..202fdc7ff 100644 --- a/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php +++ b/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php @@ -145,12 +145,12 @@ class ObjectHydrator extends AbstractHydrator { $oid = spl_object_hash($entity); $relation = $class->associationMappings[$fieldName]; - + $value = $class->reflFields[$fieldName]->getValue($entity); if ($value === null) { $value = new ArrayCollection; } - + if ( ! $value instanceof PersistentCollection) { $value = new PersistentCollection( $this->_em, @@ -161,17 +161,19 @@ class ObjectHydrator extends AbstractHydrator $class->reflFields[$fieldName]->setValue($entity, $value); $this->_uow->setOriginalEntityProperty($oid, $fieldName, $value); $this->_initializedCollections[$oid . $fieldName] = $value; - } else if (isset($this->_hints[Query::HINT_REFRESH])) { - // Is already PersistentCollection, but REFRESH + } else if (isset($this->_hints[Query::HINT_REFRESH]) || + isset($this->_hints['fetched'][$class->name][$fieldName]) && + ! $value->isInitialized()) { + // Is already PersistentCollection, but either REFRESH or FETCH-JOIN and UNINITIALIZED! $value->setDirty(false); $value->setInitialized(true); $value->unwrap()->clear(); $this->_initializedCollections[$oid . $fieldName] = $value; } else { - // Is already PersistentCollection, and DONT REFRESH + // Is already PersistentCollection, and DON'T REFRESH or FETCH-JOIN! $this->_existingCollections[$oid . $fieldName] = $value; } - + return $value; } diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC812Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC812Test.php new file mode 100644 index 000000000..3bb0a51fd --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC812Test.php @@ -0,0 +1,48 @@ +useModelSet('cms'); + parent::setUp(); + } + + /** + * @group DDC-812 + */ + public function testFetchJoinInitializesPreviouslyUninitializedCollectionOfManagedEntity() + { + //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); + $article = new CmsArticle; + $article->topic = "hello"; + $article->text = "talk talk talk"; + + $comment = new CmsComment; + $comment->topic = "good!"; + $comment->text = "stuff!"; + $comment->article = $article; + + $this->_em->persist($article); + $this->_em->persist($comment); + $this->_em->flush(); + $this->_em->clear(); + + $article2 = $this->_em->find(get_class($article), $article->id); + + $article2Again = $this->_em->createQuery( + "select a, c from Doctrine\Tests\Models\CMS\CmsArticle a join a.comments c where a.id = ?1") + ->setParameter(1, $article->id) + ->getSingleResult(); + + $this->assertTrue($article2Again === $article2); + $this->assertTrue($article2Again->comments->isInitialized()); + } +}