Ver código fonte

[Tree] MaterializedPath: Added level support and some tests

comfortablynumb 13 anos atrás
pai
commit
9eebefa09a

+ 12 - 2
lib/Gedmo/Tree/Strategy/AbstractMaterializedPath.php

@@ -157,9 +157,19 @@ abstract class AbstractMaterializedPath implements Strategy
         }
         }
 
 
         $pathProp->setValue($node, $path);
         $pathProp->setValue($node, $path);
-        $uow->scheduleExtraUpdate($node, array(
+        $changes = array(
             $config['path'] => array(null, $path)
             $config['path'] => array(null, $path)
-        ));
+        );
+
+        if (isset($config['level'])) {
+            $level = substr_count($path, $config['path_separator']);
+            $levelProp = $meta->getReflectionProperty($config['level']);
+            $levelProp->setAccessible(true);
+            $levelProp->setValue($node, $level);
+            $changes[$config['level']] = array(null, $level);
+        }
+
+        $uow->scheduleExtraUpdate($node, $changes);
         $ea->setOriginalObjectProperty($uow, $oid, $config['path'], $path);
         $ea->setOriginalObjectProperty($uow, $oid, $config['path'], $path);
     }
     }
 
 

+ 11 - 1
tests/Gedmo/Tree/MaterializedPathODMMongoDBTest.php

@@ -71,6 +71,10 @@ class MaterializedPathODMMongoDBTest extends BaseTestCaseMongoODM
         $this->assertEquals($this->generatePath(array('1', '2')), $category2->getPath());
         $this->assertEquals($this->generatePath(array('1', '2')), $category2->getPath());
         $this->assertEquals($this->generatePath(array('1', '2', '3')), $category3->getPath());
         $this->assertEquals($this->generatePath(array('1', '2', '3')), $category3->getPath());
         $this->assertEquals($this->generatePath(array('4')), $category4->getPath());
         $this->assertEquals($this->generatePath(array('4')), $category4->getPath());
+        $this->assertEquals(1, $category->getLevel());
+        $this->assertEquals(2, $category2->getLevel());
+        $this->assertEquals(3, $category3->getLevel());
+        $this->assertEquals(1, $category4->getLevel());
 
 
         // Update
         // Update
         $category2->setParent(null);
         $category2->setParent(null);
@@ -85,6 +89,10 @@ class MaterializedPathODMMongoDBTest extends BaseTestCaseMongoODM
         $this->assertEquals($this->generatePath(array('1')), $category->getPath());
         $this->assertEquals($this->generatePath(array('1')), $category->getPath());
         $this->assertEquals($this->generatePath(array('2')), $category2->getPath());
         $this->assertEquals($this->generatePath(array('2')), $category2->getPath());
         $this->assertEquals($this->generatePath(array('2', '3')), $category3->getPath());
         $this->assertEquals($this->generatePath(array('2', '3')), $category3->getPath());
+        $this->assertEquals(1, $category->getLevel());
+        $this->assertEquals(1, $category2->getLevel());
+        $this->assertEquals(2, $category3->getLevel());
+        $this->assertEquals(1, $category4->getLevel());
 
 
         // Remove
         // Remove
         $this->dm->remove($category);
         $this->dm->remove($category);
@@ -92,9 +100,11 @@ class MaterializedPathODMMongoDBTest extends BaseTestCaseMongoODM
         $this->dm->flush();
         $this->dm->flush();
 
 
         $result = $this->dm->createQueryBuilder()->find(self::CATEGORY)->getQuery()->execute();
         $result = $this->dm->createQueryBuilder()->find(self::CATEGORY)->getQuery()->execute();
+        $firstResult = $result->getNext();
 
 
         $this->assertEquals(1, $result->count());
         $this->assertEquals(1, $result->count());
-        $this->assertEquals('4', $result->getNext()->getTitle());
+        $this->assertEquals('4', $firstResult->getTitle());
+        $this->assertEquals(1, $firstResult->getLevel());
     }
     }
 
 
     public function createCategory()
     public function createCategory()