浏览代码

[Tree] ensure remove operation handles node synchronization well, closes #229

gedi 13 年之前
父节点
当前提交
e34b021558
共有 2 个文件被更改,包括 29 次插入2 次删除
  1. 1 2
      lib/Gedmo/Tree/Strategy/ORM/Nested.php
  2. 28 0
      tests/Gedmo/Tree/NestedTreeRootTest.php

+ 1 - 2
lib/Gedmo/Tree/Strategy/ORM/Nested.php

@@ -190,6 +190,7 @@ class Nested implements Strategy
         }
         $rootId = isset($config['root']) ? $wrapped->getPropertyValue($config['root']) : null;
         $diff = $rightValue - $leftValue + 1;
+        $this->shiftRL($em, $config['useObjectClass'], $rightValue + 1, -$diff, $rootId);
         if ($diff > 2) {
             $dql = "SELECT node FROM {$config['useObjectClass']} node";
             $dql .= " WHERE node.{$config['left']} BETWEEN :left AND :right";
@@ -205,8 +206,6 @@ class Nested implements Strategy
                 $uow->scheduleForDelete($removalNode);
             }
         }
-
-        $this->shiftRL($em, $config['useObjectClass'], $rightValue + 1, -$diff, $rootId);
     }
 
     /**

+ 28 - 0
tests/Gedmo/Tree/NestedTreeRootTest.php

@@ -30,6 +30,34 @@ class NestedTreeRootTest extends BaseTestCaseORM
         $this->populate();
     }
 
+    /**
+     * @test
+     */
+    function shouldRemoveAndSynchronize()
+    {
+        $repo = $this->em->getRepository(self::CATEGORY);
+        $vegies = $repo->findOneByTitle('Vegitables');
+
+        $this->em->remove($vegies);
+        $this->em->flush();
+
+        $food = $repo->findOneByTitle('Food');
+
+        $this->assertEquals(1, $food->getLeft());
+        $this->assertEquals(4, $food->getRight());
+
+        $vegies = new RootCategory;
+        $vegies->setTitle('Vegies');
+        $repo->persistAsFirstChildOf($vegies, $food);
+
+        $this->em->flush();
+        $this->assertEquals(1, $food->getLeft());
+        $this->assertEquals(6, $food->getRight());
+
+        $this->assertEquals(2, $vegies->getLeft());
+        $this->assertEquals(3, $vegies->getRight());
+    }
+
     /*public function testHeavyLoad()
     {
         $start = microtime(true);