Prechádzať zdrojové kódy

[Tree] Added getNodesHierarchyQuery and getNodesHierarchyQueryBuilder to all strategies

comfortablynumb 13 rokov pred
rodič
commit
82e64254ba

+ 22 - 0
lib/Gedmo/Tree/Document/MongoDB/Repository/AbstractTreeRepository.php

@@ -110,6 +110,28 @@ abstract class AbstractTreeRepository extends DocumentRepository
      */
      */
     abstract protected function validate();
     abstract protected function validate();
 
 
+    /**
+     * Returns a QueryBuilder configured to return an array of nodes suitable for buildTree method
+     *
+     * @param object - Root node
+     * @param bool - Obtain direct children?
+     * @param array $config
+     * @param array $options
+     * @return \Doctrine\ORM\QueryBuilder
+     */
+    abstract public function getNodesHierarchyQueryBuilder($node, $direct, array $config, array $options = array());
+
+    /**
+     * Returns a Query configured to return an array of nodes suitable for buildTree method
+     *
+     * @param object - Root node
+     * @param bool - Obtain direct children?
+     * @param array - Metadata configuration
+     * @param array - Options
+     * @return \Doctrine\ORM\Query
+     */
+    abstract public function getNodesHierarchyQuery($node, $direct, array $config, array $options = array());
+
     /**
     /**
      * Returns an array of nodes suitable for method buildTree
      * Returns an array of nodes suitable for method buildTree
      *
      *

+ 35 - 10
lib/Gedmo/Tree/Document/MongoDB/Repository/MaterializedPathRepository.php

@@ -24,7 +24,7 @@ class MaterializedPathRepository extends AbstractTreeRepository
     /**
     /**
      * Get tree query builder
      * Get tree query builder
      *
      *
-     * @return Doctrine\ODM\MongoDB\QueryBuilder
+     * @return \Doctrine\ODM\MongoDB\Query\Builder
      */
      */
     public function getTreeQueryBuilder()
     public function getTreeQueryBuilder()
     {
     {
@@ -34,7 +34,7 @@ class MaterializedPathRepository extends AbstractTreeRepository
     /**
     /**
      * Get tree query
      * Get tree query
      *
      *
-     * @return Doctrine\ODM\MongoDB\Query\Query
+     * @return \Doctrine\ODM\MongoDB\Query\Query
      */
      */
     public function getTreeQuery()
     public function getTreeQuery()
     {
     {
@@ -44,7 +44,7 @@ class MaterializedPathRepository extends AbstractTreeRepository
     /**
     /**
      * Get tree
      * Get tree
      *
      *
-     * @return Doctrine\ODM\MongoDB\Cursor
+     * @return \Doctrine\ODM\MongoDB\Cursor
      */
      */
     public function getTree()
     public function getTree()
     {
     {
@@ -54,7 +54,7 @@ class MaterializedPathRepository extends AbstractTreeRepository
     /**
     /**
      * Get all root nodes query builder
      * Get all root nodes query builder
      *
      *
-     * @return Doctrine\ODM\MongoDB\QueryBuilder
+     * @return \Doctrine\ODM\MongoDB\Query\Builder
      */
      */
     public function getRootNodesQueryBuilder($sortByField = null, $direction = 'asc')
     public function getRootNodesQueryBuilder($sortByField = null, $direction = 'asc')
     {
     {
@@ -64,7 +64,7 @@ class MaterializedPathRepository extends AbstractTreeRepository
     /**
     /**
      * Get all root nodes query
      * Get all root nodes query
      *
      *
-     * @return Doctrine\ODM\MongoDB\Query\Query
+     * @return \Doctrine\ODM\MongoDB\Query\Query
      */
      */
     public function getRootNodesQuery($sortByField = null, $direction = 'asc')
     public function getRootNodesQuery($sortByField = null, $direction = 'asc')
     {
     {
@@ -74,7 +74,7 @@ class MaterializedPathRepository extends AbstractTreeRepository
     /**
     /**
      * Get all root nodes
      * Get all root nodes
      *
      *
-     * @return Doctrine\ODM\MongoDB\Cursor
+     * @return \Doctrine\ODM\MongoDB\Cursor
      */
      */
     public function getRootNodes($sortByField = null, $direction = 'asc')
     public function getRootNodes($sortByField = null, $direction = 'asc')
     {
     {
@@ -84,7 +84,7 @@ class MaterializedPathRepository extends AbstractTreeRepository
     /**
     /**
      * Get children from node
      * Get children from node
      *
      *
-     * @return Doctrine\ODM\MongoDB\QueryBuilder
+     * @return \Doctrine\ODM\MongoDB\Query\Builder
      */
      */
     public function getChildrenQueryBuilder($node = null, $direct = false, $sortByField = null, $direction = 'asc')
     public function getChildrenQueryBuilder($node = null, $direct = false, $sortByField = null, $direction = 'asc')
     {
     {
@@ -124,7 +124,7 @@ class MaterializedPathRepository extends AbstractTreeRepository
     /**
     /**
      * Get children query
      * Get children query
      *
      *
-     * @return Doctrine\ODM\MongoDB\Query\Query
+     * @return \Doctrine\ODM\MongoDB\Query\Query
      */
      */
     public function getChildrenQuery($node = null, $direct = false, $sortByField = null, $direction = 'asc')
     public function getChildrenQuery($node = null, $direct = false, $sortByField = null, $direction = 'asc')
     {
     {
@@ -134,19 +134,44 @@ class MaterializedPathRepository extends AbstractTreeRepository
     /**
     /**
      * Get children
      * Get children
      *
      *
-     * @return Doctrine\ODM\MongoDB\Cursor
+     * @return \Doctrine\ODM\MongoDB\Cursor
      */
      */
     public function getChildren($node = null, $direct = false, $sortByField = null, $direction = 'asc')
     public function getChildren($node = null, $direct = false, $sortByField = null, $direction = 'asc')
     {
     {
         return $this->getChildrenQuery($node, $direct, $sortByField, $direction)->execute();
         return $this->getChildrenQuery($node, $direct, $sortByField, $direction)->execute();
     }
     }
 
 
+    /**
+     * {@inheritDoc}
+     */
+    public function getNodesHierarchyQueryBuilder($node, $direct, array $config, array $options = array())
+    {
+        $sortBy = array(
+            'field'     => null,
+            'dir'       => 'asc'
+        );
+
+        if (isset($options['childSort'])) {
+            $sortBy = array_merge($sortBy, $options['childSort']);
+        }
+
+        return $this->getChildrenQueryBuilder($node, $direct, $sortBy['field'], $sortBy['dir']);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function getNodesHierarchyQuery($node, $direct, array $config, array $options = array())
+    {
+        return $this->getNodesHierarchyQueryBuilder($node, $direct, $config, $options)->getQuery();
+    }
+
     /**
     /**
      * {@inheritDoc}
      * {@inheritDoc}
      */
      */
     public function getNodesHierarchy($node, $direct, array $config, array $options = array())
     public function getNodesHierarchy($node, $direct, array $config, array $options = array())
     {
     {
-        $query = $this->getChildrenQuery();
+        $query = $this->getNodesHierarchyQuery($node, $direct, $config, $options);
         $query->setHydrate(false);
         $query->setHydrate(false);
 
 
         return $query->toArray();
         return $query->toArray();

+ 22 - 0
lib/Gedmo/Tree/Entity/Repository/AbstractTreeRepository.php

@@ -110,6 +110,28 @@ abstract class AbstractTreeRepository extends EntityRepository
      */
      */
     abstract protected function validate();
     abstract protected function validate();
 
 
+    /**
+     * Returns a Query Builder configured to return an array of nodes suitable for buildTree method
+     *
+     * @param object - Root node
+     * @param bool - Obtain direct children?
+     * @param array $config
+     * @param array $options
+     * @return \Doctrine\ODM\MongoDB\Query\Builder
+     */
+    abstract public function getNodesHierarchyQueryBuilder($node, $direct, array $config, array $options = array());
+
+    /**
+     * Returns a Query configured to return an array of nodes suitable for buildTree method
+     *
+     * @param object - Root node
+     * @param bool - Obtain direct children?
+     * @param array - Metadata configuration
+     * @param array - Options
+     * @return \Doctrine\ODM\MongoDB\Query\Query
+     */
+    abstract public function getNodesHierarchyQuery($node, $direct, array $config, array $options = array());
+
     /**
     /**
      * Returns an array of nodes suitable for method buildTree
      * Returns an array of nodes suitable for method buildTree
      *
      *

+ 6 - 0
lib/Gedmo/Tree/Entity/Repository/ClosureTreeRepository.php

@@ -338,11 +338,17 @@ class ClosureTreeRepository extends AbstractTreeRepository
         return $this->getNodesHierarchyQuery($node, $direct, $config, $options)->getArrayResult();
         return $this->getNodesHierarchyQuery($node, $direct, $config, $options)->getArrayResult();
     }
     }
 
 
+    /**
+     * {@inheritdoc}
+     */
     public function getNodesHierarchyQuery($node, $direct, array $config, array $options = array())
     public function getNodesHierarchyQuery($node, $direct, array $config, array $options = array())
     {
     {
         return $this->getNodesHierarchyQueryBuilder($node, $direct, $config, $options)->getQuery();
         return $this->getNodesHierarchyQueryBuilder($node, $direct, $config, $options)->getQuery();
     }
     }
 
 
+    /**
+     * {@inheritdoc}
+     */
     public function getNodesHierarchyQueryBuilder($node, $direct, array $config, array $options = array())
     public function getNodesHierarchyQueryBuilder($node, $direct, array $config, array $options = array())
     {
     {
         $meta = $this->getClassMetadata();
         $meta = $this->getClassMetadata();

+ 26 - 1
lib/Gedmo/Tree/Entity/Repository/MaterializedPathRepository.php

@@ -147,12 +147,37 @@ class MaterializedPathRepository extends AbstractTreeRepository
         return $this->getChildrenQuery($node, $direct, $sortByField, $direction)->execute();
         return $this->getChildrenQuery($node, $direct, $sortByField, $direction)->execute();
     }
     }
 
 
+    /**
+     * {@inheritdoc}
+     */
+    public function getNodesHierarchyQueryBuilder($node, $direct, array $config, array $options = array())
+    {
+        $sortBy = array(
+            'field'     => null,
+            'dir'       => 'asc'
+        );
+
+        if (isset($options['childSort'])) {
+            $sortBy = array_merge($sortBy, $options['childSort']);
+        }
+
+        return $this->getChildrenQueryBuilder($node, $direct, $sortBy['field'], $sortBy['dir']);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getNodesHierarchyQuery($node, $direct, array $config, array $options = array())
+    {
+        return $this->getNodesHierarchyQueryBuilder($node, $direct, $config, $options)->getQuery();
+    }
+
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */
     public function getNodesHierarchy($node, $direct, array $config, array $options = array())
     public function getNodesHierarchy($node, $direct, array $config, array $options = array())
     {
     {
-        return $this->getChildrenQuery()->getArrayResult();
+        return $this->getNodesHierarchyQuery($node, $direct, $config, $options)->getArrayResult();
     }
     }
 
 
     /**
     /**

+ 20 - 4
lib/Gedmo/Tree/Entity/Repository/NestedTreeRepository.php

@@ -836,16 +836,32 @@ class NestedTreeRepository extends AbstractTreeRepository
     }
     }
 
 
     /**
     /**
-     * {@inheritdoc}
+     * {@inheritDoc}
      */
      */
-    public function getNodesHierarchy($node, $direct, array $config, array $options = array())
+    public function getNodesHierarchyQueryBuilder($node, $direct, array $config, array $options = array())
     {
     {
-        return $this->childrenQuery(
+        return $this->childrenQueryBuilder(
             $node,
             $node,
             $direct,
             $direct,
             isset($config['root']) ? array($config['root'], $config['left']) : $config['left'],
             isset($config['root']) ? array($config['root'], $config['left']) : $config['left'],
             'ASC'
             'ASC'
-        )->getArrayResult();
+        );
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function getNodesHierarchyQuery($node, $direct, array $config, array $options = array())
+    {
+        return $this->getNodesHierarchyQueryBuilder($node, $direct, $config, $options)->getQuery();
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getNodesHierarchy($node, $direct, array $config, array $options = array())
+    {
+        return $this->getNodesHierarchyQuery($node, $direct, $config, $options)->getArrayResult();
     }
     }
 
 
     /**
     /**

+ 1 - 2
tests/Gedmo/Tree/MaterializedPathODMMongoDBRepositoryTest.php

@@ -102,8 +102,7 @@ class MaterializedPathODMMongoDBRepositoryTest extends BaseTestCaseMongoODM
     function childrenHierarchy()
     function childrenHierarchy()
     {
     {
         $repo = $this->dm->getRepository(self::CATEGORY);
         $repo = $this->dm->getRepository(self::CATEGORY);
-        $roots = $repo->getRootNodes();
-        $tree = $repo->childrenHierarchy($roots->getNext());
+        $tree = $repo->childrenHierarchy();
 
 
         $vegitablesChildren = $tree[0]['__children'][1]['__children'];
         $vegitablesChildren = $tree[0]['__children'][1]['__children'];
         $this->assertEquals('Food', $tree[0]['title']);
         $this->assertEquals('Food', $tree[0]['title']);

+ 1 - 2
tests/Gedmo/Tree/MaterializedPathORMRepositoryTest.php

@@ -104,8 +104,7 @@ class MaterializedPathORMRepositoryTest extends BaseTestCaseORM
     public function testBuildTreeMethod()
     public function testBuildTreeMethod()
     {
     {
         $repo = $this->em->getRepository(self::CATEGORY);
         $repo = $this->em->getRepository(self::CATEGORY);
-        $roots = $repo->getRootNodes();
-        $tree = $repo->childrenHierarchy($roots[0]);
+        $tree = $repo->childrenHierarchy();
 
 
         $vegitables = $tree[0]['__children'][1];
         $vegitables = $tree[0]['__children'][1];