Gedmo树getPath错误:节点与此存储库无关500内部服务器错误-InvalidArgumentException [英] Gedmo Tree getPath Error: Node is not related to this repository 500 Internal Server Error - InvalidArgumentException
问题描述
我遇到错误:
Node is not related to this repository
500 Internal Server Error - InvalidArgumentException
更新1:是否设置树扩展抽象存储库错误是相同的.
UPDATE 1: it does not matter if I set up tree repository with traits or extend abstract repository the error is the same.
更新2:完整堆栈跟踪 http://pastebin.com/TtaJnyzf
UPDATE 2: Full stack trace http://pastebin.com/TtaJnyzf
我想从数据库中输出具有树结构的html树,特别是我需要获取从根到所选节点的路径.据我了解,这是通过getPath()函数完成的.
I want to output html tree with tree structure from database and specifically I need to get path from root to selected node. As I understand it, that is done with getPath() function.
我正在使用:
- Symfony v3.0.6;
- Doctrine v2.5.4
- StofDoctrineExtensionsBundle [1]
以管理树结构.
要设置树形结构,我使用了Symfony.com [2]上的文档,然后使用了GitHub [3],[4],[5],[6]上的文档.
To setup Tree structure I used documentation on Symfony.com [2] followed by documentation on GitHub [3], [4], [5], [6].
到目前为止,我在数据库中有一个树形结构,并且我得到了这样的html树:
So far I have a tree structure in database and i get html tree 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));
}
}
我更改了两行以显示从树元素的根到所选项目的路径
I changed two lines in order to display path from the root of an tree element to selected item
nodeDecorator' => function($node) use ($repo)
{
return '<a href="/project_path/'. implode('/', $repo->getPath($node)) .'">'. $node['title'] .'</a>';
}
如[7]和[8]中所示,存在应该将元素数组从根返回到所选项目的getPath()函数.
As seen in [7] and [8] the function getPath() that supposed to return array of elements from the root to the selected item exists.
我认为问题可能出在以下代码块中:
I think the problem may lie in this code block:
$repo->getPath($node)
- [1] stofDoctrineExtensionsBundle 在GitHub上;
- [2]在Symfony.com上 stofDoctrineExtensinsBundnle文档;
- [3] GitHub上的 Gedmo树文档;
- [4] Gedmo树>树实体例子;
- [5] Gedmo树>基本用法例子;
- [6] 树html输出例子;
- [7] NestedTreeRepository使用 NestedTreeRepositoryTrait
- [8] NestedTreeRepositoryTrait具有函数"getPath()" .
- [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] Tree html output example;
- [7] NestedTreeRepository uses NestedTreeRepositoryTrait
- [8] NestedTreeRepositoryTrait has function "getPath()".
请告知. 谢谢您的时间和知识.
Please advise. Thank you for your time and knowledge.
推荐答案
让它正常工作!
以下是所需的更改:
代替
nodeDecorator' => function($node) use ($repo)
{
return '<a href="/project_path/'. implode('/', $repo->getPath($node)) .'">'. $node['title'] .'</a>';
}
一个人应该写
'nodeDecorator' => function($node) use ($repo)
{
return '<a href="/project_path/'. @implode('/', $repo->getPath($repo->findOneBy(array('id' => $node['id'])))) .'">'. $node['title'] .'</a>';
}
并在Category类中添加
and in Category class add
public function __toString()
{
return $this->getTitle();
}
就是这样,现在应该显示每个节点的路径.
That is it, path to each node now should be showing.
这篇关于Gedmo树getPath错误:节点与此存储库无关500内部服务器错误-InvalidArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!