How to give container as argument to services

It’s easy, if service extends ContainerAware use \Symfony\Component\DependencyInjection\ContainerAware; class YouService extends ContainerAware { public function someMethod() { $this->container->get(‘router’)->generate(‘fos_user_profile_edit’) … } } service.yml your.service: class: App\…\YouService calls: – [ setContainer,[ @service_container ] ]

How to access a different controller from inside a controller Symfony2

If you don’t want to define the class as a service, as it doesn’t feel as a good practice to me and @Qoop quoted Fabien saying the same, you can use forwarding: http://symfony.com/doc/current/controller/forwarding.html public function indexAction($name) { $response = $this->forward(‘AcmeHelloBundle:Hello:fancy’, array( ‘name’ => $name, ‘color’ => ‘green’, )); // … further modify the response or … Read more

Doctrine query builder using inner join with conditions

I’m going to answer my own question. innerJoin should use the keyword “WITH” instead of “ON” (Doctrine’s documentation [13.2.6. Helper methods] is inaccurate; [13.2.5. The Expr class] is correct) no need to link foreign keys in join condition as they’re already specified in the entity mapping. Therefore, the following works for me $qb->select(‘c’) ->innerJoin(‘c.phones’, ‘p’, … Read more

How to inject a repository into a service in Symfony?

Here is a cleaned up solution for those coming from Google like me: Update: here is the Symfony 2.6 (and up) solution: services: myrepository: class: Doctrine\ORM\EntityRepository factory: [“@doctrine.orm.entity_manager”, getRepository] arguments: – MyBundle\Entity\MyClass myservice: class: MyBundle\Service\MyService arguments: – “@myrepository” Deprecated solution (Symfony 2.5 and less): services: myrepository: class: Doctrine\ORM\EntityRepository factory_service: doctrine.orm.entity_manager factory_method: getRepository arguments: – MyBundle\Entity\MyClass … Read more

Accessing Files Relative to Bundle in Symfony2

As a matter of fact, there is a service you could use for this, the kernel ($this->get(‘kernel’)). It has a method called locateResource(). For example: $kernel = $container->getService(‘kernel’); $path = $kernel->locateResource(‘@AdmeDemoBundle/path/to/file/Foo.txt’);