Gedmo Tree getPath 错误:节点与此存储库不相关 500 内部服务器错误 - InvalidArgumentException

2024-02-13

我收到错误:

Node is not related to this repository
500 Internal Server Error - InvalidArgumentException

更新1:如果我设置树并不重要具有特征的存储库 https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/tree.md#tree-repositories or 扩展抽象存储库 https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/tree.md#extend-abstract-repositores错误是一样的。

更新 2:完整的堆栈跟踪http://pastebin.com/TtaJnyzf http://pastebin.com/TtaJnyzf

我想从数据库中输出具有树结构的 html 树,特别是我需要获取从根到选定节点的路径。据我了解,这是通过 getPath() 函数完成的。

我在用:

  • Symfony v3.0.6;
  • 教义 v2.5.4
  • StofDoctrineExtensionsBundle [1]

为了管理树结构。

为了设置树结构,我使用了 Symfony.com [2] 上的文档,然后使用了 GitHub [3]、[4]、[5]、[6] 上的文档。

到目前为止,我在数据库中有一个树结构,我得到这样的 html 树:

<?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) use ($repo)
{
    return '<a href="/project_path/'. implode('/', $repo->getPath($node)) .'">'. $node['title'] .'</a>';
}

如 [7] 和 [8] 中所示,函数 getPath() 应该返回从根到所选项目的元素数组。

我认为问题可能出在这个代码块上:

$repo->getPath($node)
  • [1] stofDoctrineExtensionsBundle https://github.com/stof/StofDoctrineExtensionsBundle在 GitHub 上;
  • [2] stofDoctrineExtensinsBundnle 文档 http://symfony.com/doc/current/bundles/StofDoctrineExtensionsBundle/index.html在 Symfony.com 上;
  • [3] Gedmo 树文档 https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/tree.md在 GitHub 上;
  • [4] Gedmo 树 > 树实体示例 https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/tree.md#tree-entity-example;
  • [5] Gedmo 树 > 基本使用示例 https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/tree.md#basic-usage-examples;
  • [6] 树 html 输出示例 https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/tree.md#customize-html-tree-output;
  • [7] NestedTreeRepository使用嵌套树存储库特征 https://github.com/Atlantic18/DoctrineExtensions/blob/master/lib/Gedmo/Tree/Entity/Repository/NestedTreeRepository.php
  • [8] NestedTreeRepositoryTrait 有函数“getPath()” https://github.com/Atlantic18/DoctrineExtensions/blob/master/lib/Gedmo/Tree/Traits/Repository/ORM/NestedTreeRepositoryTrait.php.

请指教。 感谢您的时间和知识。


成功了!

以下是所需的更改:

代替

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();
}

就是这样,现在应该显示到每个节点的路径。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Gedmo Tree getPath 错误:节点与此存储库不相关 500 内部服务器错误 - InvalidArgumentException 的相关文章

随机推荐