서비스의 Symfony 2 EntityManager 삽입
내 자신의 서비스를 만들었고 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"
config.yml
내 앱에서 .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를 가져오고 유효한 (null이 아닌) EntityManager 개체를 가져 오는 데 사용하는 동일한 컨트롤러에서.
Look like that I'm missing piece of configuration or some link between UserService and Doctrine config.
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;
...
}
}
Since 2017 and Symfony 3.3 you can register Repository as service, with all its advantages it has.
Check my post How to use Repository with Doctrine as Service in Symfony for more general description.
To your specific case, original code with tuning would 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 new custom 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. Register services
# app/config/services.yml
services:
_defaults:
autowire: true
Test\CommonBundle\:
resource: ../../Test/CommonBundle
참고URL : https://stackoverflow.com/questions/10427282/symfony-2-entitymanager-injection-in-service
'Programing' 카테고리의 다른 글
파이프 ''을 (를) 찾을 수 없습니다. angular2 사용자 지정 파이프 (0) | 2020.08.26 |
---|---|
JSLint : 정의되기 전에 사용되었습니다. (0) | 2020.08.26 |
Bash : 이름이 지정된 파일을 재귀 적으로 복사하여 폴더 구조를 유지합니다. (0) | 2020.08.26 |
지도 저장 (0) | 2020.08.26 |
Express.js에서 Node.js를 사용하는 세션은 어떻게 작동합니까? (0) | 2020.08.26 |