浏览代码

[tree-nestedset] allow moving up and down root nodes for trees without root field

gediminasm 14 年之前
父节点
当前提交
6ef15c2ea3
共有 2 个文件被更改,包括 32 次插入8 次删除
  1. 16 8
      lib/Gedmo/Tree/Entity/Repository/NestedTreeRepository.php
  2. 16 0
      tests/Gedmo/Tree/RepositoryTest.php

+ 16 - 8
lib/Gedmo/Tree/Entity/Repository/NestedTreeRepository.php

@@ -354,17 +354,21 @@ class NestedTreeRepository extends AbstractTreeRepository
 
         $config = $this->listener->getConfiguration($this->_em, $meta->name);
         $parent = $wrapped->getPropertyValue($config['parent']);
-        if (!$parent) {
+        if (isset($config['root']) && !$parent) {
             throw new InvalidArgumentException("Cannot get siblings from tree root node");
         }
-        $wrappedParent = new EntityWrapper($parent, $this->_em);
-        $parentId = $wrappedParent->getIdentifier();
 
         $left = $wrapped->getPropertyValue($config['left']);
         $sign = $includeSelf ? '>=' : '>';
 
         $dql = "SELECT node FROM {$config['useObjectClass']} node";
-        $dql .= " WHERE node.{$config['parent']} = {$parentId}";
+        if ($parent) {
+            $wrappedParent = new EntityWrapper($parent, $this->_em);
+            $parentId = $wrappedParent->getIdentifier();
+            $dql .= " WHERE node.{$config['parent']} = {$parentId}";
+        } else {
+            $dql .= " WHERE node.{$config['parent']} IS NULL";
+        }
         $dql .= " AND node.{$config['left']} {$sign} {$left}";
         $dql .= " ORDER BY node.{$config['left']} ASC";
         return $this->_em->createQuery($dql);
@@ -403,17 +407,21 @@ class NestedTreeRepository extends AbstractTreeRepository
 
         $config = $this->listener->getConfiguration($this->_em, $meta->name);
         $parent = $wrapped->getPropertyValue($config['parent']);
-        if (!$parent) {
+        if (isset($config['root']) && !$parent) {
             throw new InvalidArgumentException("Cannot get siblings from tree root node");
         }
-        $wrappedParent = new EntityWrapper($parent, $this->_em);
-        $parentId = $wrappedParent->getIdentifier();
 
         $left = $wrapped->getPropertyValue($config['left']);
         $sign = $includeSelf ? '<=' : '<';
 
         $dql = "SELECT node FROM {$config['useObjectClass']} node";
-        $dql .= " WHERE node.{$config['parent']} = {$parentId}";
+        if ($parent) {
+            $wrappedParent = new EntityWrapper($parent, $this->_em);
+            $parentId = $wrappedParent->getIdentifier();
+            $dql .= " WHERE node.{$config['parent']} = {$parentId}";
+        } else {
+            $dql .= " WHERE node.{$config['parent']} IS NULL";
+        }
         $dql .= " AND node.{$config['left']} {$sign} {$left}";
         $dql .= " ORDER BY node.{$config['left']} ASC";
         return $this->_em->createQuery($dql);

+ 16 - 0
tests/Gedmo/Tree/RepositoryTest.php

@@ -306,6 +306,22 @@ class RepositoryTest extends BaseTestCaseORM
         //$this->assertTrue($repo->verify());
     }
 
+    public function testMoveRootNode()
+    {
+        $repo = $this->em->getRepository(self::CATEGORY);
+        $food = $repo->findOneByTitle('Food');
+
+        $repo->moveDown($food, 1);
+
+        $meta = $this->em->getClassMetadata(self::CATEGORY);
+
+        $left = $meta->getReflectionProperty('lft')->getValue($food);
+        $right = $meta->getReflectionProperty('rgt')->getValue($food);
+
+        $this->assertEquals($left, 3);
+        $this->assertEquals($right, 12);
+    }
+
     protected function getUsedEntityFixtures()
     {
         return array(