【发布时间】:2017-02-04 22:58:00
【问题描述】:
我正在使用 Symfony v3.0.4、Doctrine v2.5.4 和 StofDoctrineExtensionsBundle [1] 来管理树结构。
为了设置树形结构,我使用了 Symfony.com [2] 上的文档,然后是 GitHub [3] 上的文档。
然后我继续进行树设置 - 使用示例 [4] 中的树实体并使用 [5] 中的代码创建树。
我没有使用 [6] 和 [7],因为它似乎没有必要(据我所知,没有它的树可以工作和显示)。查看更新。
到目前为止,我在数据库中有一个树形结构,为了显示它,我修改了示例 [8]。像这样:
<?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));
}
}
如果我这样修改一行:
'nodeDecorator' => function($node) {
return '<a href="/project_path/'. implode('/', getPath($node)) .'">'. $node['title'] .'</a>';
}
为了获得树的每个元素的路径,我得到了错误:
Fatal error: Call to undefined function AppBundle\Controller\getPath()
500 Internal Server Error - FatalThrowableError
- [1]stofDoctrineExtensionsBundle on GitHub;
- [2]stofDoctrineExtensinsBundnle documentation on Symfony.com;
- [3]Gedmo Tree documentation on GitHub;
- [4]Gedmo Tree > Tree Entity example;
- [5]Gedmo Tree > Basic Usage Example;
- [6]DID NOT USE: Gedmo Tree > Tree Repositories;
- [7]Gedmo Tree > Use Abstract Repositories;
- [8]Tree html output example;
- [9]NestedTreeRepository has method getPath().
如 [9] 中所见,NestedTreeRepositry 有一个方法 getPath()。
更新:我尝试了 [6] 但未能成功设置它。然后我尝试 [7] 设置好,但仍然错误仍然存在!
请指教。 感谢您的时间和知识。
【问题讨论】:
标签: doctrine-orm tree symfony doctrine-extensions stofdoctrineextensions