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.