Selaa lähdekoodia

[tree-nestedset] simulated changeset when parent does not change using persistAs*, closes #108

gediminasm 14 vuotta sitten
vanhempi
commit
e45140d57b

+ 1 - 0
lib/Gedmo/Tree/Entity/Repository/NestedTreeRepository.php

@@ -87,6 +87,7 @@ class NestedTreeRepository extends AbstractTreeRepository
                 $wrapped->setPropertyValue($config['parent'], $parent);
                 $position = substr($position, 0, -2);
             }
+            $wrapped->setPropertyValue($config['left'], 0); // simulate changeset
             $oid = spl_object_hash($node);
             $this->listener
                 ->getStrategy($this->_em, $meta->name)

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

@@ -136,8 +136,15 @@ class Nested implements Strategy
         if (isset($config['root']) && isset($changeSet[$config['root']])) {
             throw new \Gedmo\Exception\UnexpectedValueException("Root cannot be changed manualy, change parent instead");
         }
-        if (isset($changeSet[$config['parent']])) {
-            $this->updateNode($em, $node, $changeSet[$config['parent']][1]);
+        if (isset($changeSet[$config['parent']]) || isset($changeSet[$config['left']])) {
+            $oid = spl_object_hash($node);
+            $wrapped = AbstractWrapper::wrapp($node, $em);
+            $parent = $wrapped->getPropertyValue($config['parent']);
+            if (isset($this->nodePositions[$oid]) && isset($changeSet[$config['left']])) {
+                // revert simulated changeset
+                $wrapped->setPropertyValue($config['left'], $changeSet[$config['left']][0]);
+            }
+            $this->updateNode($em, $node, $parent);
         }
     }
 

+ 17 - 0
tests/Gedmo/Tree/NestedTreePositionTest.php

@@ -60,6 +60,23 @@ class NestedTreePositionTest extends BaseTestCaseORM
         $this->assertEquals(11, $fruits->getRight());
     }
 
+    public function testTreeChildPositionMove()
+    {
+        $this->populate();
+        $repo = $this->em->getRepository(self::ROOT_CATEGORY);
+
+        $oranges = $repo->findOneByTitle('Oranges');
+        $fruits = $repo->findOneByTitle('Fruits');
+
+        $this->assertEquals(2, $oranges->getLevel());
+
+        $repo->persistAsNextSiblingOf($oranges, $fruits);
+        $this->em->flush();
+
+        $this->assertEquals(1, $oranges->getLevel());
+        $this->assertEquals(1, count($repo->children($fruits, true)));
+    }
+
     public function testOnRootCategory()
     {
         // need to check if this does not produce errors