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

报告相同问题?

悬赏问题

  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。