Skip to content

Commit 585bddb

Browse files
committed
Merge pull request #1547 from Slamdunk/hotfix/closure-em
[Tree] Closure cannot handle different EntityManagers
2 parents 5f4b6c8 + a4e076e commit 585bddb

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

lib/Gedmo/Tree/Strategy/ORM/Closure.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public function onFlushEnd($em, AdapterInterface $ea)
170170
*/
171171
public function processPrePersist($em, $node)
172172
{
173-
$this->pendingChildNodeInserts[spl_object_hash($node)] = $node;
173+
$this->pendingChildNodeInserts[spl_object_hash($em)][spl_object_hash($node)] = $node;
174174
}
175175

176176
/**
@@ -237,8 +237,9 @@ public function processPostRemove($em, $entity, AdapterInterface $ea)
237237
public function processPostPersist($em, $entity, AdapterInterface $ea)
238238
{
239239
$uow = $em->getUnitOfWork();
240+
$emHash = spl_object_hash($em);
240241

241-
while ($node = array_shift($this->pendingChildNodeInserts)) {
242+
while ($node = array_shift($this->pendingChildNodeInserts[$emHash])) {
242243
$meta = $em->getClassMetadata(get_class($node));
243244
$config = $this->listener->getConfiguration($em, $meta->name);
244245

@@ -256,7 +257,7 @@ public function processPostPersist($em, $entity, AdapterInterface $ea)
256257

257258
$referenceMapping = $em->getClassMetadata($config['closure'])->getAssociationMapping('ancestor');
258259
$referenceId = $referenceMapping['sourceToTargetKeyColumns'][$ancestorColumnName];
259-
260+
260261
$entries = array(
261262
array(
262263
$ancestorColumnName => $nodeId,
@@ -348,7 +349,7 @@ protected function setLevelFieldOnPendingNodes(ObjectManager $em)
348349
$sql .= 'GROUP BY c.descendant';
349350

350351
$levelsAssoc = $em->getConnection()->executeQuery($sql, array(array_keys($this->pendingNodesLevelProcess)), array($type))->fetchAll(\PDO::FETCH_NUM);
351-
352+
352353
//create key pair array with resultset
353354
$levels = array();
354355
foreach( $levelsAssoc as $level )

tests/Gedmo/Tree/ClosureTreeTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Gedmo\Tree;
44

55
use Doctrine\Common\EventManager;
6+
use Gedmo\Tree\TreeListener;
67
use Tool\BaseTestCaseORM;
78
use Tree\Fixture\Closure\Category;
89
use Tree\Fixture\Closure\News;
@@ -351,4 +352,30 @@ public function testCascadePersistTree()
351352

352353
$this->assertCount(1, $closure);
353354
}
355+
356+
public function testPersistOnRightEmInstance()
357+
{
358+
$evm = new EventManager();
359+
$evm->addEventSubscriber(new TreeListener());
360+
361+
$emOne = $this->getMockSqliteEntityManager($evm);
362+
$emTwo = $this->getMockSqliteEntityManager($evm);
363+
364+
$categoryOne = new Category();
365+
$categoryOne->setTitle('Politics');
366+
367+
$categoryTwo = new Category();
368+
$categoryTwo->setTitle('Politics');
369+
370+
// Persist and Flush on different times !
371+
$emOne->persist($categoryOne);
372+
373+
$emTwo->persist($categoryTwo);
374+
$emTwo->flush();
375+
376+
$emOne->flush();
377+
378+
$this->assertNotNull($categoryOne->getId());
379+
$this->assertNotNull($categoryTwo->getId());
380+
}
354381
}

0 commit comments

Comments
 (0)