Symfony 2 EntityManager injection in service Symfony 2 EntityManager injection in service symfony symfony

Symfony 2 EntityManager injection in service


Your class's constructor method should be called __construct(), not __constructor():

public function __construct(EntityManager $entityManager){    $this->em = $entityManager;}


For modern reference, in Symfony 2.4+, you cannot name the arguments for the Constructor Injection method anymore. According to the documentation You would pass in:

services:    test.common.userservice:        class:  Test\CommonBundle\Services\UserService        arguments: [ "@doctrine.orm.entity_manager" ]

And then they would be available in the order they were listed via the arguments (if there are more than 1).

public function __construct(EntityManager $entityManager) {    $this->em = $entityManager;}


Note as of Symfony 3.3 EntityManager is depreciated. Use EntityManagerInterface instead.

namespace AppBundle\Service;use Doctrine\ORM\EntityManagerInterface;class Someclass {    protected $em;    public function __construct(EntityManagerInterface $entityManager)    {        $this->em = $entityManager;    }    public function somefunction() {        $em = $this->em;        ...    }}