douqiao7958
2016-04-11 06:32
浏览 57
已采纳

Symfony - 在表单的EventSubscriber中注入实体管理器

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 EventSubscriber AddDepartmentDegreeCourseFieldSubscriber which needs access to 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'
        ));
    }
}

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

Catchable Fatal Error: Argument 1 passed to AppBundle\Form\EventListener\AddDepartmentDegreeCourseFieldSubscriber::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in /Users/shairyar/Sites/oxford-portal/src/AppBundle/Form/UserType.php on line 21 and defined

I know what the error means but I thought the service i registered in services.yml should inject the EntityManager so why I am getting this error? What am i missing here? Any help will be really appreciated.

图片转代码服务由CSDN问答提供 功能建议

这是我第一次使用表单的EventListener,所以我在努力学习如何在其中注入EntityManager 。

我有一个名为 UserType 的formType,在这个类中我有一个需要访问EntityManager的EventSubscriber AddDepartmentDegreeCourseFieldSubscriber

 类UserType扩展AbstractType 
 {
 / ** 
 * @param FormBuilderInterface $ builder 
 * @param array $ options 
 * / 
 n public function buildForm(FormBuilderInterface $  builder,array $ options)
 {
 $ builder-> addEventSubscriber(new AddProfileFieldSubscriber()); 
 $ builder-> addEventSubscriber(new AddDepartmentDegreeCourseFieldSubscriber()); 
} 
 
 / ** \  n * @param OptionsResolver $ resolver 
 * / 
公共函数configureOptions(OptionsResolver $ resolver)
 {
 $ resolver-> setDefaults(array(
'data_class'=>'AppBundle \ Entity \ Us 呃'
)); 
} 
} 
   
 
 

这是我的 services.yml \ n

  app.department_course_degree_subscriber:
 class:AppBundle \ Form \ EventListener \ AddDepartmentDegreeCourseFieldSubscriber 
 arguments:[“@ doctrine.orm.entity_manager”] 
 tags:
  -  {name:kernel.event_subscriber}  
   
 
 

我得到的错误如下:

捕获致命错误:参数1传递给 AppBundle \ Form \ EventListener \ AddDepartmentDegreeCourseFieldSubscriber :: __ construct() 必须是Doctrine \ ORM \ EntityManager的实例,没有给出,在/ Users / shairyar / Sites / oxford-portal / src / AppBundle / Form / UserType中称为 .php 在第21行并定义了

我知道错误意味着什么,但我认为我在 services.yml 中注册的服务 应该注入 EntityManager ,为什么我收到此错误? 我在这里失踪了什么? 任何帮助都将非常感激。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题