php Symfony 2 EntityManager 注入服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10427282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Symfony 2 EntityManager injection in service
提问by Andrey Zavarin
I've created my own service and I need to inject doctrine EntityManager, but I don't see that __construct()is called on my service, and injection doesn't work.
我已经创建了自己的服务,我需要注入学说 EntityManager,但我没有看到__construct()我的服务调用了它,并且注入不起作用。
Here is 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.ymlin my bundle
这是services.yml在我的包里
services:
test.common.userservice:
class: Test\CommonBundle\Services\UserService
arguments:
entityManager: "@doctrine.orm.entity_manager"
I've imported that .yml in config.ymlin my app like that
我已经config.yml在我的应用程序中导入了那个 .yml
imports:
# a few lines skipped, not relevant here, i think
- { resource: "@TestCommonBundle/Resources/config/services.yml" }
And when I call service in controller
当我在控制器中调用服务时
$userservice = $this->get('test.common.userservice');
$userservice->getUser(123);
I get an object (not null), but $this->emin UserService is null, and as I already mentioned, constructor on UserService has never been called
我得到一个对象(非空),但$this->em在 UserService 中为空,正如我已经提到的,从未调用过 UserService 上的构造函数
One more thing, Controller and UserService are in different bundles (I really need that to keep project organized), but still: everyting else works fine, I can even call
还有一件事,Controller 和 UserService 在不同的包中(我真的需要它来保持项目的组织性),但仍然:其他一切正常,我什至可以调用
$this->get('doctrine.orm.entity_manager')
in same controller that I use to get UserService and get valid (not null) EntityManager object.
在我用来获取 UserService 并获取有效(非空)EntityManager 对象的同一个控制器中。
Look like that I'm missing piece of configuration or some link between UserService and Doctrine config.
看起来我缺少配置或 UserService 和 Doctrine 配置之间的某些链接。
回答by richsage
Your class's constructor method should be called __construct(), not __constructor():
你的类的构造函数方法应该被调用__construct(),而不是__constructor():
public function __construct(EntityManager $entityManager)
{
$this->em = $entityManager;
}
回答by Chadwick Meyer
For modern reference, in Symfony 2.4+, you cannot name the arguments for the Constructor Injection method anymore. According to the documentationYou would pass in:
作为现代参考,在 Symfony 2.4+ 中,您不能再为构造函数注入方法命名参数。根据您将传入的文档:
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).
然后它们将按照通过参数列出的顺序提供(如果有超过 1 个)。
public function __construct(EntityManager $entityManager) {
$this->em = $entityManager;
}
回答by Robert Saylor
Note as of Symfony 3.3 EntityManager is depreciated. Use EntityManagerInterface instead.
注意 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;
...
}
}
回答by Tomá? Votruba
Since 2017 and Symfony 3.3you can register Repository as service, with all its advantages it has.
从 2017 年和 Symfony 3.3 开始,您可以将 Repository 注册为 service,以及它的所有优点。
Check my post How to use Repository with Doctrine as Service in Symfonyfor more general description.
查看我的文章How to use Repository with Doctrine as Service in Symfony以获得更一般的描述。
To your specific case, original code with tuning would look like this:
对于您的具体情况,经过调整的原始代码如下所示:
1. Use in your services or Controller
1. 在您的服务或控制器中使用
<?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
2.创建新的自定义存储库
<?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
3. 注册服务
# app/config/services.yml
services:
_defaults:
autowire: true
Test\CommonBundle\:
resource: ../../Test/CommonBundle

