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:我是否设置树并不重要 具有特征的存储库 或 extend抽象存储库 错误是一样的.
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;
- 教义 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 AppBundleController;
use AppBundleEntityCategory;
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationRequest;
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] GitHub 上的 stofDoctrineExtensionsBundle;
- [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>';
}
并在类别类中添加
public function __toString()
{
return $this->getTitle();
}
就是这样,现在应该显示每个节点的路径.
That is it, path to each node now should be showing.
这篇关于Gedmo 树 getPath 错误:节点与此存储库无关 500 内部服务器错误 - InvalidArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!