问题描述
这是我第一次使用表单的EventListener,所以我正在努力如何在其中注入EntityManager。
我有这个formType称为 UserType
,在这个类中,我有一个EventSubscriber AddDepartmentDegreeCourseFieldSubscriber
需要访问EntityManager
class UserType extends AbstractType
{
/ **
* @param FormBuilderInterface $ builder
* @param array $ options
* /
public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder-> addEventSubscriber(new AddProfileFieldSubscriber());
$ builder-> addEventSubscriber(new AddDepartmentDegreeCourseFieldSubscriber());
}
/ **
* @param OptionsResolver $ resolver
* /
public function configureOptions(OptionsResolver $ resolver)
{
$ resolver-> setDefaults(array(
'data_class'=>'AppBundle\Entity\User'
));
}
}
这是我的服务。 yml
app.department_course_degree_subscriber:
class:AppBundle\Form\EventListener\\ \\AddDepartmentDegreeCourseFieldSubscriber
arguments:[@ doctrine.orm.entity_manager]
标签:
- {name:kernel.event_subscriber}
我得到的错误如下
我在...之间的shairyar / Sites / oxford-portal / src / AppBundle / Form / UserType.php
知道错误是什么意思,但我认为我在 services.yml
中注册的服务应该注入 EntityManager
所以为什么我得到这个错误?我在这里失踪了任何帮助将非常感激。
这是因为,您传递新的实例AddDepartmentDegreeCourseFieldSubscriber
建筑形式。您需要从服务容器传递实例。
使用AppBundle\Form\EventListener\ AddDepartmentDegreeCourseFieldSubscriber;
class UserType extends AbstractType
{
private $ addDepartmentDegreeCourseFieldSubscriber;
public function __construct(AddDepartmentDegreeCourseFieldSubscriber $ subscriber)
{
$ this-> addDepartmentDegreeCourseFieldSubscriber = $ subscriber;
}
/ **
* @param FormBuilderInterface $ builder
* @param数组$ options
* /
public function buildForm FormBuilderInterface $ builder,array $ options)
{
$ builder-> addEventSubscriber($ this-> addDepartmentDegreeCourseFieldSubscriber);
}
}
#app / config / services.yml
/ pre>
services:
app.department_course_degree_subscriber:
class:AppBundle\Form\EventListener\AddDepartmentDegreeCourseFieldSubscriber
arguments:[ @ doctrine.orm.entity_manager]
标签:
- {name:kernel.event_subscriber}
app.form.type.my_user_form:
class:AppBundle \Form\UserType
参数:[@ app.department_course_degree_subscriber]
标签:
- {name:form.type}
This is the first time I am working with EventListener of a form so I am struggling on how to inject EntityManager in it.
I have this formType called
UserType
and in this class I have an EventSubscriberAddDepartmentDegreeCourseFieldSubscriber
which needs access to EntityManagerclass UserType extends AbstractType { /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder->addEventSubscriber(new AddProfileFieldSubscriber()); $builder->addEventSubscriber(new AddDepartmentDegreeCourseFieldSubscriber()); } /** * @param OptionsResolver $resolver */ public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'data_class' => 'AppBundle\Entity\User' )); } }
This is my
services.yml
app.department_course_degree_subscriber: class: AppBundle\Form\EventListener\AddDepartmentDegreeCourseFieldSubscriber arguments: ["@doctrine.orm.entity_manager"] tags: - { name: kernel.event_subscriber }
The error I get is as following
I know what the error means but I thought the service i registered in
services.yml
should inject theEntityManager
so why I am getting this error? What am i missing here? Any help will be really appreciated.解决方案It's because, you pass new instance of
AddDepartmentDegreeCourseFieldSubscriber
when building form. You need to pass instance from service container.use AppBundle\Form\EventListener\AddDepartmentDegreeCourseFieldSubscriber; class UserType extends AbstractType { private $addDepartmentDegreeCourseFieldSubscriber; public function __construct(AddDepartmentDegreeCourseFieldSubscriber $subscriber) { $this->addDepartmentDegreeCourseFieldSubscriber = $subscriber; } /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder->addEventSubscriber($this->addDepartmentDegreeCourseFieldSubscriber); } }
# app/config/services.yml services: app.department_course_degree_subscriber: class: AppBundle\Form\EventListener\AddDepartmentDegreeCourseFieldSubscriber arguments: ["@doctrine.orm.entity_manager"] tags: - { name: kernel.event_subscriber } app.form.type.my_user_form: class: AppBundle\Form\UserType arguments: [ "@app.department_course_degree_subscriber" ] tags: - { name: form.type }
这篇关于Symfony - 在EventSubscriber中注入实体经理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!