【问题标题】:How to getPath of Gedmo Tree element using symfony3, doctrine2 and stofDoctrineExtensionsBundle如何使用 symfony3、doctrine2 和 stofDoctrineExtensionsBundle 获取 Gedmo 树元素的路径
【发布时间】: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

如 [9] 中所见,NestedTreeRepositry 有一个方法 getPath()。

更新:我尝试了 [6] 但未能成功设置它。然后我尝试 [7] 设置好,但仍然错误仍然存​​在!

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

【问题讨论】:

    标签: doctrine-orm tree symfony doctrine-extensions stofdoctrineextensions


    【解决方案1】:

    使用存在于

    中的函数getPath()

    "vendor/gedmo/doctrine-extensions/lib/Gedmo/Tree/Entity/Repository" in the file "NestedTreeRepository.php"

    或在

    "vendor/gedmo/doctrine-extensions/lib/Gedmo/Tree/Traits/Repository/ORM" in the file "NestedTreeRepositoryTrait.php"

    必须像这样修改代码块:

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

    添加注释

    use ($repo)

    @implode('/', $repo-&gt;getPath($repo-&gt;findOneBy(array('id' =&gt; $node['id']))))

    有了这个 - 应该没有 undefined function 错误

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,因此我改用了getPathInfo(),效果很好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-29
        • 1970-01-01
        • 2019-09-02
        • 1970-01-01
        相关资源
        最近更新 更多