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.

  • 写回答

1条回答 默认 最新

  • duanpo2037 2016-04-11 08:14
    关注

    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 }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题