Я создал свою собственную службу, и мне нужно ввести доктрину EntityManager, но я не вижу, что на мой сервис вызывается __construct()
, и инъекция не работает.
Вот код и конфиги:
<?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
}
}
Вот services.yml
в моем пакете
services:
test.common.userservice:
class: Test\CommonBundle\Services\UserService
arguments:
entityManager: "@doctrine.orm.entity_manager"
Я импортировал этот .yml в config.yml
в мое приложение, подобное этому
imports:
# a few lines skipped, not relevant here, i think
- { resource: "@TestCommonBundle/Resources/config/services.yml" }
И когда я вызываю службу в контроллере
$userservice = $this->get('test.common.userservice');
$userservice->getUser(123);
Я получаю объект (но не null), но $this->em
в UserService имеет значение null, и, как я уже упоминал, конструктор в UserService никогда не назывался
Еще одна вещь: Controller и UserService находятся в разных пакетах (мне действительно нужно, чтобы организовать проект), но все же: все, что работает, отлично работает, я даже могу позвонить
$this->get('doctrine.orm.entity_manager')
в том же контроллере, который я использую, чтобы получить UserService и получить действительный (не пустой) объект EntityManager.
Похоже, что мне не хватает части конфигурации или некоторой связи между UserService и конфигурацией Doctrine.
Ваш метод конструктора классов следует называть __construct()
, а не __constructor()
:
public function __construct(EntityManager $entityManager)
{
$this->em = $entityManager;
}
Для современной справки в Symfony 2.4+ вы больше не можете использовать аргументы для метода инжекции конструктора. В соответствии с документацией вы должны пройти:
services:
test.common.userservice:
class: Test\CommonBundle\Services\UserService
arguments: [ "@doctrine.orm.entity_manager" ]
И тогда они будут доступны в том порядке, в котором они были указаны через аргументы (если их больше 1).
public function __construct(EntityManager $entityManager) {
$this->em = $entityManager;
}
Примечание по состоянию на Symfony 3.3 EntityManager обесценивается. Вместо этого используйте EntityManagerInterface.
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;
...
}
}
С 2017 года и Symfony 3.3 вы можете зарегистрировать репозиторий как сервис со всеми его преимуществами.
Отметьте мой пост Как использовать репозиторий с Doctrine как услугу в Symfony для более общего описания.
В вашем конкретном случае исходный код с настройкой будет выглядеть так:
<?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);
}
}
<?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);
}
}
# app/config/services.yml
services:
_defaults:
autowire: true
Test\CommonBundle\:
resource: ../../Test/CommonBundle