How to inject Doctrine Entity Manager into Symfony 4 Service

20,316

Solution 1

Use only in Symfony 4.

use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Name; //if you use entity for example Name

class ExampleService{
    private $em;
    public function __construct(EntityManagerInterface $em)
    {
        $this->em = $em;
    }

    function newName($code) // for example with a entity
    {
        $name = new Name();
        $name->setCode($code); // use setter for entity

        $this->em->persist($name);
        $this->em->flush();
    }
}

Solution 2

I know it's an old post, but just in case somebody struggles with this, there's a typo in the use statment:

use Doctrine\ORM\EntityManagerInterface: //<- see that's a colon, not a semicolon

Solution 3

Agree with Yarimadam. Service container, dependency injection and autowiring is not a story about injecting into methods. Dependencies injected into objects we are calling "services".

When application is up, service container is built injecting one services into another ones via class constructor or "set" method invocation.

Your ExampleController::someFunction is intended to be called only by you, so only way how this method will receive $injectedService as an argument, is that you will pass it evidently. This is the wrong way.

Solution 4

A classic symfony service with autowiring uses constructor injection method to inject dependencies. In your case, you don't have a constructor.

You may consider to add a constructor method and set dependency to a private class property. And use accordingly.

Or you can utilize setter injection.

Service Configuration:

services:
 app.example_controller:
     class: Your\Namespace\ExampleController
     calls:
         - [setExampleService, ['@exampleService']]

Controller Class:

class ExampleController
{
    private $exampleService;

    public function someFunction() {
        $this->exampleService->serviceFunction();
    }

    public function setExampleService(ExampleService $exampleService) {
        $this->exampleService = $exampleService;
    }
}
Share:
20,316
BLaZuRE
Author by

BLaZuRE

I've only been programming for a few years. I primarily deal with, PHP, MySQL, Java, and C++.

Updated on March 01, 2020

Comments

  • BLaZuRE
    BLaZuRE about 4 years

    I have a controller

    use Doctrine\ORM\EntityManagerInterface:
    class ExampleController{
       public function someFunction(ExampleService $injectedService){
           $injectedService->serviceFunction();
        }
    }
    

    With a Service

    use Doctrine\ORM\EntityManagerInterface;
    class ExampleService{
        public function __construct(EntityManagerInterface $em){
            ...
        }    
    }
    

    However, calls to someFunction() fail due to 0 parameters being passed (the EntityManagerInterface is not being injected). I am attempting to use the EntityManager from the Service. Autowiring is on. I've tried the solutions for Symfony3 but they don't seem to work unless I'm missing something.

    Edit: Here is my services.yaml:

    services:
        _defaults:
            autowire: true
            autoconfigure: true
            public: false 
    
        App\:
            resource: '../src/*'
            exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'
    
        App\Controller\:
            resource: '../src/Controller'
            tags: ['controller.service_arguments']
    
  • BLaZuRE
    BLaZuRE about 6 years
    ExampleService class contains a constructor. Although missing from the snippet, the service has a em property and the constructor has $this->em = $em. The documentation states "You can specify [the argument in YAML]". The wording made me think this was optional. Is it in fact required?
  • Yarimadam
    Yarimadam about 6 years
    How do you execute the class method ExampleController->someFunction ? Could you provide the contex? If you execute this method manually you have to pass the dependency. Thats the expected behaviour.
  • TangMonk
    TangMonk over 4 years
    But error throw: MyService::__construct() must implement interface Doctrine\ORM\EntityManagerInterface, string given