簡體   English   中英

EventSubscriber中的Symfony訪問實體管理器

[英]Symfony access Entity Manager inside EventSubscriber

我有一份雇主資料表格,我正在嘗試將Employer所在的StateCity這部分工作正常,但FormType有這么長時間,這個功能將在網站的其他地方需要,所以我決定搬家這個邏輯在EventSubscriber中,並在我需要的地方重用它。

我遇到的問題是,我試圖將自己的頭放在如何在EventSubscriber類內注入EntityManager

我知道我可以在我的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);
    }
}

我得到的錯誤是

可捕獲的致命錯誤:傳遞給AppBundle \\ Form \\ EventListener \\ AddStateFieldSubscriber :: __ construct()的參數1必須是Doctrine \\ ORM \\ EntityManager的實例,沒有給出,在/ Users / shairyar / Sites / clickjobboard / src / AppBundle / Form中調用/EmployerProfileType.php在第48行並已定義

我通過服務注入EntityManager然后為什么我會收到此錯誤?

如果在EmployerProfileType里面我替換了

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

事情開始正常。

我以為在Symfony中我們應該通過創建服務來注入依賴項? 那么如何將EntityManager注入AddStateFieldSubscriber()類中

我究竟做錯了什么? 可能是我在思考它。

將不勝感激任何反饋。

我認為您需要將AddStateFieldSubscriber標記為kernel.event_subscriber。

然后,根本不需要表單內的綁定。

相反,您需要檢查正確的表單,如果表單不是使用該訂閱者的表單之一,則跳出事件偵聽器方法,因為事件將在任何表單上觸發,而不僅僅是EmployerProfileType表單。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM