Symfony访问EventSubscriber中的实体管理器

时间:2016-03-20 07:56:13

标签: php doctrine-orm dependency-injection symfony

我有一份雇主资料表单,我正在尝试将State Employer所在的CityEntityManager相关联,此部分工作正常,但FormType变长了这个在网站的其他地方需要这个功能,所以我决定在EventSubscriber中移动这个逻辑,并在需要的地方重复使用它。

我遇到的问题是我正试图解决如何在EventSubscriber类中注入services.yml

我知道我可以在我的app.form.location: class: AppBundle\Form\EventListener\AddStateFieldSubscriber arguments: ['@doctrine.orm.entity_manager'] tags: - { name: kernel.event_subscriber } 中添加以下代码,并且应该这样做但它不起作用。

EmployerProfileType

这是我的addEventSubscriber,我在呼叫AddStateFieldSubscriber()class EmployerProfileType extends AbstractType { protected $em; function __construct(EntityManager $em) { $this->em = $em; } public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('firstName', TextType::class) ->add('lastName', TextType::class) ->add('companyName', TextType::class) ->add('companyProfile', TextareaType::class) ->add('companyLogo', FileType::class, array( 'data_class' => null )); $builder->addEventSubscriber(new AddStateFieldSubscriber()); } public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'data_class' => 'AppBundle\Entity\EmployerProfile', )); } public function getName() { return 'app_bundle_emp_profile_type'; } }

AddStateFieldSubscriber

这是我的EntityManager课程,我需要访问class AddStateFieldSubscriber implements EventSubscriberInterface { protected $em; function __construct(EntityManager $em) { $this->em = $em; } public static function getSubscribedEvents() { // Tells the dispatcher that you want to listen on the form.pre_set_data // event and that the preSetData method should be called. return array( FormEvents::PRE_SET_DATA => 'onPreSetData', FormEvents::PRE_SUBMIT => 'onPreSubmit' ); } protected function addElements(FormInterface $form, States $province = null) { // Remove the submit button, we will place this at the end of the form later // Add the province element $form->add('state', EntityType::class, array( 'data' => $province, 'placeholder' => 'provide_state', 'class' => 'AdminBundle\Entity\States', 'mapped' => false) ); // Cities are empty, unless we actually supplied a province $cities = array(); if ($province) { // Fetch the cities from specified province $repo = $this->em->getRepository('AdminBundle:Cities'); $cities = $repo->findByStates($province, array('name' => 'asc')); } // Add the city element $form->add('city', EntityType::class, array( 'placeholder' => 'provide_state_first', 'class' => 'AdminBundle\Entity\Cities', 'choices' => $cities, )); } function onPreSubmit(FormEvent $event) { $form = $event->getForm(); $data = $event->getData(); // Note that the data is not yet hydrated into the entity. $province = $this->em->getRepository('AdminBundle:States')->find($data['state']); $this->addElements($form, $province); } function onPreSetData(FormEvent $event) { $account = $event->getData(); $form = $event->getForm(); // We might have an empty account (when we insert a new account, for instance) $province = $account->getCity() ? $account->getCity()->getStates() : null; $this->addElements($form, $province); } }

EntityManager

我得到的错误是

  

捕获致命错误:参数1传递给   的appbundle \表格\事件监听\ AddStateFieldSubscriber :: __结构()   必须是Doctrine \ ORM \ EntityManager的一个实例,没有给定,调用   在   /Users/shairyar/Sites/clickjobboard/src/AppBundle/Form/EmployerProfileType.php   在第48行并定义

我通过服务注入EmployerProfileType然后为什么会出现此错误?

如果在$builder->addEventSubscriber(new AddStateFieldSubscriber();内,我会替换

$builder->addEventSubscriber(new AddStateFieldSubscriber($this->em));EntityManager

然后事情开始正常。

我认为在Symfony中我们应该通过创建服务注入依赖项?那么如何在AddStateFieldSubscriber()

中注入{{1}}

我做错了什么?可能是我在思考它。

欢迎任何反馈。

1 个答案:

答案 0 :(得分:0)

我认为您需要将AddStateFieldSubscriber标记为kernel.event_subscriber。

然后表单内部的绑定根本不需要。

相反,您需要检查正确的表单,如果表单不是使用该订阅者的表单之一,则跳出事件侦听器方法,因为事件将在任何表单上触发,而不仅仅是EmployerProfileType表单