I am using Symfony v3.0.4, Doctrine v2.5.4 and StofDoctrineExtensionsBundle [1] in order to manage Tree structure.
To setup Tree structure I used documentation on Symfony.com [2] followed by documentatnion on GitHub [3].
Then I proceeded with tree setup - used tree entity from example [4] and used code in [5] to create a tree.
I did not use [6] and [7] as it seems not necessary (as far as I can tell tree works and displays without it). See update.
So far I have a tree structure in database and to display it I modified example [8]. Like this:
<?php
namespace AppBundle\Controller;
use AppBundle\Entity\Category;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class TreeController extends Controller
{
/**
* @Route("/tree", name="tree")
*/
public function treeAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository('AppBundle:Category');
$options = array(
'decorate' => true,
'rootOpen' => '<ul>',
'rootClose' => '</ul>',
'childOpen' => '<li>',
'childClose' => '</li>',
'nodeDecorator' => function($node) {
return '<a href="/some_path/...">'. $node['title'] .'</a>';
}
);
$htmlTree = $repo->childrenHierarchy(
null, /* starting from root nodes */
false, /* false: load all children, true: only direct */
$options
);
return $this->render('tree/tree_show.html.twig', array('project_tree' => $htmlTree));
}
}
if I modify one line like this:
'nodeDecorator' => function($node) {
return '<a href="/project_path/'. implode('/', getPath($node)) .'">'. $node['title'] .'</a>';
}
in order to get path to each element of the tree i get error:
Fatal error: Call to undefined function AppBundle\Controller\getPath()
500 Internal Server Error - FatalThrowableError
As seen in [9] NestedTreeRepositry has a method getPath().
Update: I tried [6] but did not manage to set it up. Then I tried [7] set it up ok, but still error remains the same!
Please advise. Thank you for your time and knowledge.
To use functon getPath()
that exists in
"vendor/gedmo/doctrine-extensions/lib/Gedmo/Tree/Entity/Repository" in the file "NestedTreeRepository.php"
or in
"vendor/gedmo/doctrine-extensions/lib/Gedmo/Tree/Traits/Repository/ORM" in the file "NestedTreeRepositoryTrait.php"
one has to modify code block like this:
nodeDecorator' => function($node) use ($repo)
{
return '<a href="/project_path/'. @implode('/', $repo->getPath($repo->findOneBy(array('id' => $node['id'])))) .'">'. $node['title'] .'</a>';
}
note added
use ($repo)
and
@implode('/', $repo->getPath($repo->findOneBy(array('id' => $node['id']))))
with this - there should be no undefined function
error