4

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.

Shairyar
  • 3,268
  • 7
  • 46
  • 86
  • How do you create the Form object? You have to use the container to get the formType. – codeneuss Apr 11 '16 at 06:43
  • @v.eigler form in created in a controller `$profileForm = $this->createForm(UserType::class, $userInfo);` if i understood your question right. – Shairyar Apr 11 '16 at 06:45
  • duplicate: http://stackoverflow.com/questions/24876767/inject-symfony-entitymanager-into-form-type-via-services – codeneuss Apr 11 '16 at 06:46
  • thanks for sharing the link, once i have the `EntityManager` inside the `FormType` how do i pass that to the subscriber class? – Shairyar Apr 11 '16 at 06:48
  • In the constructor. You have to pass it to a private/protected var in the form-object and than inject it to the class in your bulidForm-Method. – codeneuss Apr 11 '16 at 06:51
  • can you give me an example? – Shairyar Apr 11 '16 at 06:52
  • like `$builder->addEventSubscriber(new AddDepartmentDegreeCourseFieldSubscriber($this->em));` – codeneuss Apr 11 '16 at 06:53
  • yes i thought that should do and it is working but is this the right way of doing it? by right i mean the `symfony` way since we are supposed to be injecting dependencies rather than manually doing it – Shairyar Apr 11 '16 at 06:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108782/discussion-between-v-eigler-and-baig). – codeneuss Apr 11 '16 at 06:55

1 Answers1

5

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 }
jkucharovic
  • 4,214
  • 1
  • 31
  • 46
  • that is actually the solution i was talking about. I've tried it but think that there is a problem with the EventSubscriberInterface because the getSubscribedEvents() method is static. – codeneuss Apr 11 '16 at 08:30