Putting Symfony 2 EntityManager Into Operation

I created my own service, and I need to introduce the EntityManager doctrine, but I don’t see that __construct() is being called on my service, and the injection does not work.

Here are the code and configs:

 <?php namespace Test\CommonBundle\Services; use Doctrine\ORM\EntityManager; class UserService { /** * * @var EntityManager */ protected $em; public function __constructor(EntityManager $entityManager) { var_dump($entityManager); exit(); // I've never saw it happen, looks like constructor never called $this->em = $entityManager; } public function getUser($userId){ var_dump($this->em ); // outputs null } } 

Here is services.yml in my package

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

I imported this .yml to config.yml into my application like this

 imports: # a few lines skipped, not relevant here, i think - { resource: "@TestCommonBundle/Resources/config/services.yml" } 

And when I call the service in the controller

  $userservice = $this->get('test.common.userservice'); $userservice->getUser(123); 

I get an object (but not null), but $this->em in UserService is null, and as I mentioned, the constructor in UserService was never called

One more thing: Controller and UserService are in different packages (I really need to organize the project), but still: everything that works, works fine, I can even call

 $this->get('doctrine.orm.entity_manager') 

in the same controller that I use to get the UserService and get a valid (not empty) EntityManager object.

It seems like I am missing a piece of configuration or some connection between UserService and Doctrine configuration.

+83
php dependency-injection symfony
May 03 '12 at 7:54 AM
source share
4 answers

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

 public function __construct(EntityManager $entityManager) { $this->em = $entityManager; } 
+97
May 03 '12 at 7:58 a.m.
source share

For modern help in Symfony 2.4+, you can no longer use the arguments for the constructor injection method. In accordance with the documentation you must go through:

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

And then they will be available in the order in which they were indicated through the arguments (if there are more than 1).

 public function __construct(EntityManager $entityManager) { $this->em = $entityManager; } 
+60
Jul 22 '14 at 0:09
source share

Note as of Symfony 3.3 EntityManager is depreciating. 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; ... } } 
+9
Aug 29 '17 at 20:22
source share

Since 2017 and Symfony 3.3, you can register a repository as a service with all its advantages.

Check out my post How to Use Doctrine Repository as a Service in Symfony for a more general description.




In your specific case, the source code with the configuration will look like this:

1. Use in your services or controller

 <?php namespace Test\CommonBundle\Services; use Doctrine\ORM\EntityManagerInterface; class UserService { private $userRepository; // use custom repository over direct use of EntityManager // see step 2 public function __constructor(UserRepository $userRepository) { $this->userRepository = $userRepository; } public function getUser($userId) { return $this->userRepository->find($userId); } } 

2. Create a new user repository

 <?php namespace Test\CommonBundle\Repository; use Doctrine\ORM\EntityManagerInterface; class UserRepository { private $repository; public function __construct(EntityManagerInterface $entityManager) { $this->repository = $entityManager->getRepository(UserEntity::class); } public function find($userId) { return $this->repository->find($userId); } } 

3. Registration of services

 # app/config/services.yml services: _defaults: autowire: true Test\CommonBundle\: resource: ../../Test/CommonBundle 
+4
Oct 21 '17 at 11:03 on
source share



All Articles