generateUrl在childrenHierarchy中不起作用

时间:2013-09-13 12:46:07

标签: symfony tree nested-sets symfony-2.3 doctrine-extensions

在doctrine extensions tree中使用generateUrl

行动

$repo = $em->getRepository('Entity\Category');
    $options = array(
        'decorate' => true,
        'rootOpen' => '<ul>',
        'rootClose' => '</ul>',
        'childOpen' => '<li>',
        'childClose' => '</li>',
        'nodeDecorator' => function($node) {
            return '<a href="'.$this->generateUrl('_control_category_edit', array('id' => $node[$id])).'">'.$node[$field].'</a>';
        }
    );
    $htmlTree = $repo->childrenHierarchy(
        null, /* starting from root nodes */
        false, /* true: load all children, false: only direct */
        $options
    );

错误:

FatalErrorException:错误:当不在

中的对象上下文中时使用$ this

2 个答案:

答案 0 :(得分:0)

您必须将其注册为服务并注入@router

答案 1 :(得分:0)

节点Decorator是一个闭包,因此你不能在里面使用它。试试这个:

//depending in which context you are
$routing = $this->container->get('router');

[...]
    'nodeDecorator' => function($node) use ($router) {
        return '<a href="'.$router->generate('_control_category_edit', array('id' => $node[$id])).'">'.$node[$field].'</a>';
    }