1

In Controller I use a service with:

$this->get('version')->doSomething();

How to do the same from a Repository ?

Note: I do not want to inject the service in Repository constructor, because I use the service in only one of many Repository methods.

Guillaume
  • 162
  • 3
  • 11
  • 1
    You can't use the `$this->get(...)` in repository method. This method in controller use the service container (that is big to load) – Mcsky Oct 23 '17 at 10:25
  • Most likely you do not need to inject your service straight into repository, which should only save/read data. Take a look at CQRS and Command Bus patterns. – svgrafov Oct 23 '17 at 11:12
  • After seeing over 10 questions on same topic right here on StackOverflow, I've put my answer to the post: [How to use Repository with Doctrine as Service in Symfony](https://www.tomasvotruba.cz/blog/2017/10/16/how-to-use-repository-with-doctrine-as-service-in-symfony/). I think you'll find your answer there :) if not, just ping me and I'll answer to your specific use case. – Tomas Votruba Oct 23 '17 at 18:45
  • Possible duplicate of [Symfony 2: Creating a service from a Repository](https://stackoverflow.com/questions/17228417/symfony-2-creating-a-service-from-a-repository) – Tomas Votruba Oct 23 '17 at 18:46

2 Answers2

1

$this->get('service') requires you to inject DI container into your controller constructor. It is done behind the scene in Symfony controllers. So the two ways to access your service are either inject whole container(which is an antipattern, but everyone uses it) or inject only your service into another class.

If you do not want to inject your service into constructor, use setter injection.

services.yml:

 app.newsletter_manager:
     class: AppBundle\SomeRepository
     calls:
         - [setService, ['@service']]

Service class

class SomeRepository
{

    private $service;

    public function setService(ServiceClass $service)
    {
        $this->service = $service;
    }

    // ...
}
svgrafov
  • 1,970
  • 4
  • 16
  • 32
1

Injecting service into a repository is bad practice and you shouldn't do it.

Although if want solution which will work with get->('any.service') it will look like following:

In services.yml

app.some_service:
  class: 'AppBundle\Repositories\SomeEntityRepository'
  factory: ["@doctrine.orm.entity_manager", getRepository]
  arguments:
    - 'AppBundle\Entity\SomeEntity'
  calls:
    - [setContainer, ["@service_container"]]

In your repository class:

class SomeEntityRepository implements ContainerAwareInterface
{
    use ContainerAwareTrait;

    public function foo()
    {
        $bar = $this->container->get('app.bar_service');
    }
}

It's also better to inject individual service rather than the service container.

Łukasz D. Tulikowski
  • 1,440
  • 1
  • 17
  • 36
  • I use this solution and it works fine. A standard injection, in constructor, create the service each time. This solution is nicer because the "service" is only created when needed. Thank you. – Guillaume Nov 16 '17 at 09:03