diff --git a/lib/Doctrine/ODM/PHPCR/UnitOfWork.php b/lib/Doctrine/ODM/PHPCR/UnitOfWork.php index 70dc0d863..ab9a8fde2 100644 --- a/lib/Doctrine/ODM/PHPCR/UnitOfWork.php +++ b/lib/Doctrine/ODM/PHPCR/UnitOfWork.php @@ -437,13 +437,15 @@ public function getOrCreateDocuments($className, $nodes, array &$hints = array() } foreach ($nodes as $node) { - $id = $node->getPath(); - if (!isset($documents[$id])) { + $id = $node->getPath(); + $document = $this->getDocumentById($id) ?: (isset($documents[$id]) ? $documents[$id] : null); + + if (! $document) { continue; } - $document = $documents[$id]; - $class = $this->dm->getClassMetadata(get_class($document)); + $documents[$id] = $document; + $class = $this->dm->getClassMetadata(get_class($document)); $documentState = array(); $nonMappedData = array(); diff --git a/tests/Doctrine/Tests/ODM/PHPCR/Functional/UnitOfWorkTest.php b/tests/Doctrine/Tests/ODM/PHPCR/Functional/UnitOfWorkTest.php index dc2181e88..5aa7220dd 100644 --- a/tests/Doctrine/Tests/ODM/PHPCR/Functional/UnitOfWorkTest.php +++ b/tests/Doctrine/Tests/ODM/PHPCR/Functional/UnitOfWorkTest.php @@ -147,4 +147,42 @@ public function testComputeChangeSetForTranslatableDocument() $this->assertCount(1, $this->uow->getScheduledInserts()); $this->assertCount(0, $this->uow->getScheduledUpdates()); } + + public function testFetchingMultipleHierarchicalObjectsWithChildIdFirst() + { + $parent = new ParentTestObj(); + $parent->nodename = 'parent'; + $parent->name = 'parent'; + $parent->parent = $this->dm->find(null, 'functional'); + + $child = new ParentTestObj(); + $child->nodename = 'child'; + $child->name = 'child'; + $child->parent = $parent; + + $this->dm->persist($parent); + $this->dm->persist($child); + + $parentId = $this->uow->getDocumentId($parent); + $childId = $this->uow->getDocumentId($child); + + $this->dm->flush(); + $this->dm->clear(); + + // this forces the objects to be loaded in an order where the $parent will become a proxy + $documents = $this->dm->findMany( + 'Doctrine\Tests\Models\References\ParentTestObj', + array($childId, $parentId) + ); + + $this->assertCount(2, $documents); + + /* @var $child ParentTestObj */ + /* @var $parent ParentTestObj */ + $child = $documents->first(); + $parent = $documents->last(); + + $this->assertSame($child->parent, $parent); + $this->assertSame('parent', $parent->nodename); + } }