-1

using generateUrl in doctrine extensions tree

in action

$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
    );

error:

FatalErrorException: Error: Using $this when not in object context in

Bilegsaikhan
  • 134
  • 3
  • 11

2 Answers2

0

You have to register that as a service and inject the @router

albert
  • 4,290
  • 1
  • 21
  • 42
0

nodeDecorator is a closure, therefor you cannot use this inside. Try this:

//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>';
    }
Paul Andrieux
  • 1,836
  • 11
  • 24