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 { protected $em; public function __constructor(EntityManager $entityManager) { var_dump($entityManager); exit();
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.
php dependency-injection symfony
Andrey Zavarin May 03 '12 at 7:54 AM 2012-05-03 07:54
source share