Skip to content

Commit

Permalink
Merge pull request #581 from Ocramius/hotfix/duplicate-objects-in-ide…
Browse files Browse the repository at this point in the history
…ntity-map

Hotfix: Duplicate objects in identity map when using `findMany`
  • Loading branch information
lsmith77 committed Dec 13, 2014
2 parents d3f2c70 + 985ceba commit 320c840
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/Doctrine/ODM/PHPCR/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
38 changes: 38 additions & 0 deletions tests/Doctrine/Tests/ODM/PHPCR/Functional/UnitOfWorkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 320c840

Please sign in to comment.