Gedmo Tree getPath Error: Node is not related to this repository 500 Internal Server Error - InvalidArgumentException

怎甘沉沦 提交于 2019-12-20 07:27:41

问题


I am getting error:

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

UPDATE 1: it does not matter if I set up tree repository with traits or extend abstract repository the error is the same.

UPDATE 2: Full stack trace http://pastebin.com/TtaJnyzf

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.

I am using:

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

in order to manage Tree structure.

To setup Tree structure I used documentation on Symfony.com [2] followed by documentation on GitHub [3], [4], [5], [6].

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>';
}

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 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.


回答1:


Got it working!

Here are the needed changes:

instead of

nodeDecorator' => function($node) use ($repo)
{
    return '<a href="/project_path/'. implode('/', $repo->getPath($node)) .'">'. $node['title'] .'</a>';
}

one should write

'nodeDecorator' => function($node) use ($repo)
{
    return '<a href="/project_path/'. @implode('/', $repo->getPath($repo->findOneBy(array('id' => $node['id'])))) .'">'. $node['title'] .'</a>';
}

and in Category class add

public function __toString()
{
    return $this->getTitle();
}

That is it, path to each node now should be showing.



来源:https://stackoverflow.com/questions/37217961/gedmo-tree-getpath-error-node-is-not-related-to-this-repository-500-internal-se

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!