[英]How can I inject doctrine into Symfony2 service
I use simplified controller and have no service container. 我使用简化的控制器,没有服务容器。 I'm trying to inject doctrine into a service.
我正在尝试将学说注入服务中。 Here is the error:
这是错误:
ContextErrorException: Catchable Fatal Error: Argument 1 passed to Acme\Controller\WebController::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in /home/hcs/Core/app/cache/dev/appDevDebugProjectContainer.php on line 3036 and defined in /home/hcs/Core/src/Acme/Controller/WebController.php line 28
Here is my service def: 这是我的服务def:
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
orm:
mappings:
Acme:
type: annotation
dir: %kernel.root_dir%/../src/Acme/Model
prefix: Acme\Model
alias: Model
is_bundle: false
services:
WebController:
class: Acme\Controller\WebController
arguments: [@doctrine.orm.entity_manager ]
parent: elnur.controller.abstract
And my class 和我的班级
use Doctrine\ORM\EntityManager;
/**
* @Service("WebController", parent="elnur.controller.abstract")
*/
class WebController extends AbstractController
{
protected $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
Here is output from SF2 container (php app/console container:debug | grep -i en tity) 这是SF2容器的输出(php app / console container:debug | grep -i enity)
doctrine.orm.default_entity_manager container EntityManager5330e85ad5afb_546a8d27f194334ee012bfe64f629947b07e4919\__CG__\Doctrine\ORM\EntityManager
doctrine.orm.entity_manager n/a alias for doctrine.orm.default_entity_manager
doctrine.orm.validator.unique container Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator
form.type.entity container Symfony\Bridge\Doctrine\Form\Type\EntityType
translator container Symfony\Component\Translation\IdentityTranslator
I've also tried arguments: [ "@doctrine.orm.entity_manager" ] in the service definition and still nothing. 我也在服务定义中尝试了参数:[“@ doctrine.orm.entity_manager”]但仍然没有。 I've tried cleaning my cache, but I cannot for the life of me get an EM injected.
我已经尝试过清理我的缓存,但我不能为我的生活注入EM。 What am I doing wrong?
我究竟做错了什么?
The problem is that you're mixing both YAML service definition and DiExtra annotations for the same class. 问题是你要为同一个类混合YAML服务定义和DiExtra注释。 The YAML definition is full and provides the required dependency, but you haven't added
@InjectParams
to play with the @Service
annotation. YAML定义已满并提供了所需的依赖关系,但您尚未添加
@InjectParams
来使用@Service
批注。 Basically, you're creating two services of the single class using different approaches and the annotations approach is not complete. 基本上,您使用不同的方法创建单个类的两个服务,并且注释方法不完整。
Either remove the @Service
annotation from the class, or, if you prefer annotations (which I do), inject the dependencies with annotations too and get rid of the YAML service definition: 从类中删除
@Service
注释,或者,如果您更喜欢注释(我这样做),也可以使用注释注入依赖项并删除YAML服务定义:
/**
* @InjectParams({
* "em" = @Inject("doctrine.orm.entity_manager")
* })
*/
public function __construct(EntityManager $em)
{
$this->em = $em;
}
The @InjectParams
annotation matches parameter names like $em
to service names like @em
. @InjectParams
注释将诸如$em
类的参数名称与服务名称(如@em
相匹配。 Since there is no @em
service, you have to override the default matching with the @Inject
annotation. 由于没有
@em
服务,您必须覆盖与@Inject
批注的默认匹配。
But there is a way to simplify it. 但有一种方法可以简化它。 You can alias the entity manager to
@em
in your YAML configuration file: 您可以在YAML配置文件中将实体管理器别名为
@em
:
services:
em:
alias: doctrine.orm.entity_manager
Now you can simplify the @InjectParams
annotation: 现在您可以简化
@InjectParams
注释:
/**
* @InjectParams
*/
public function __construct(EntityManager $em)
{
$this->em = $em;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.