diff --git a/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php b/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php index ca937059b..cb33a56a6 100644 --- a/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php +++ b/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php @@ -335,6 +335,7 @@ class ObjectHydrator extends AbstractHydrator } } else if ( ! $reflField->getValue($parentObject)) { $coll = new PersistentCollection($this->_em, $this->_ce[$entityName], new ArrayCollection); + $coll->setOwner($parentObject, $relation); $reflField->setValue($parentObject, $coll); $this->_uow->setOriginalEntityProperty($oid, $relationField, $coll); } diff --git a/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php b/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php index 1fee14767..eef376baf 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php @@ -247,6 +247,34 @@ class ManyToManyBasicAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCa $this->assertEquals(0, count($newUser->getGroups())); } + /** + * @group DDC-839 + */ + public function testWorkWithDqlHydratedEmptyCollection() + { + $user = $this->addCmsUserGblancoWithGroups(0); + $group = new CmsGroup(); + $group->name = "Developers0"; + $this->_em->persist($group); + + $this->_em->flush(); + $this->_em->clear(); + + $newUser = $this->_em->createQuery('SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.groups g WHERE u.id = ?1') + ->setParameter(1, $user->getId()) + ->getSingleResult(); + $this->assertEquals(0, count($newUser->groups)); + $this->assertType('array', $newUser->groups->getMapping()); + + $newUser->addGroup($group); + + $this->_em->flush(); + $this->_em->clear(); + + $newUser = $this->_em->find(get_class($user), $user->getId()); + $this->assertEquals(1, count($newUser->groups)); + } + /** * @param int $groupCount * @return CmsUser