php - Symfony - 在表单的 EventSubscriber 中注入(inject)实体管理器

标签 php doctrine-orm symfony

这是我第一次使用表单的 EventListener,所以我正在努力研究如何在其中注入(inject) EntityManager。

我有一个名为 UserType 的 formType,在这个类中我有一个 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'
        ));
    }
}

这是我的services.yml

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

我得到的错误如下

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

我知道这个错误是什么意思,但我认为我在 services.yml 中注册的服务应该注入(inject) EntityManager 那么为什么我会收到这个错误?我在这里错过了什么?非常感谢任何帮助。

最佳答案

这是因为,您在构建表单时传递了 AddDepartmentDegreeCourseFieldSubscriber 的新实例。您需要从服务容器传递实例。

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 }

关于php - Symfony - 在表单的 EventSubscriber 中注入(inject)实体管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36541378/

相关文章:

php - PHP动态绑定(bind)参数

javascript - 单击“重新加载”图标还是单击“F5”?重定向到另一个页面

entity-framework - 如何在 websocket symfony 应用程序的服务中使用实体、表单、 Controller

mysql - Doctrine 2 - float 后两位小数?

mysql - Symfony2 查询语法错误

php - 表单的 View 数据应该是类的一个实例......但是是一个(n)字符串

php - PHP MySQL JSON 中的 REST Web 服务 - 将空值插入数据库

php - 具有相同值的数字数组

symfony - Doctrine setAutoCommit(false) 方法和 "there is no active transaction"消息

doctrine-orm - 在ZF2中扩展ZfcUserDoctrineORM的用户实体