Search code examples
symfonyknpmenubundle

KnpMenuBundle, extend Builder by ContainerAware and Controller


I m using KNPMenuBuilder to create a menu. I would like to add query in my Builder.php but I need to extend Builder class by Controller. By default it`s extends by "ContainerAware".

I would like to ask how to extends Builder class by two elements- "ContainerAware" and "Controller"? Or how can I create a DQL query without extending of Controller?


Solution

  • The Controller class is just a "tool" to easily use controllers (some people even say it's a bad thing to use, as it's somewhat advocating a service locator pattern).

    You shouldn't extend Controller in any class other than a controller.

    To get access to doctrine (like the Controller#getDoctrine() method), you should request for the doctrine service. Then you just have the same as you have when using the getDoctrine() method.

    Since the builder extends ContainerAware, you have access to a $container property and you can get() the doctrine service from it:

    $doctrine = $this->container->get('doctrine');
    $em = $doctrine->getManager();
    
    $query = $em->createQuery(...);