Not able to get children of children using StofDoctrineExtension Tree

898 views Asked by At

I have a database that was created dynamically just for trial purposes. I created a parent(home), set its children and then created children of those children(sub children you can say).

Now i want to only get the list of all the Sub-Children.

My Code

    $pages = $this->getDoctrine()->getRepository('BloggerBlogBundle:Page');
    $nodes = $pages->getChildren();
    $parent = $nodes[0]; //get Parent node 
    $rootNodes = $parent->getChildren(); // get Children of Parent Nodes

    foreach($rootNodes as $node)
    {
        $nodes = $node->getChildren(); // get Children of Children
    }

Now i am returning $nodes to my view and in return i only get sub-children of only one children node instead of all the sub-children of all the children.

What mistake i am making here? Please help, Thanks !!!

1

There are 1 answers

3
qooplmao On BEST ANSWER

You are resetting the nodes variable on each foreach.

To get all nodes you could create an array and add all subsequent nodes onto the end of that.

$pages = $this->getDoctrine()->getRepository('BloggerBlogBundle:Page');
$nodes = $pages->getChildren();
$parent = $nodes[0]; //get Parent node 
$children = $parent->getChildren(); // get Children of Parent Nodes

$subChildren = array();

foreach ($children as $child)
{
    $subChildren = array_merge($subChildren, $child->getChildren()->toArray());
}