EntityType形式的PersisCollection

时间:2018-11-06 16:07:32

标签: php forms symfony4

我将所有数据从表单保存到DB中的JSON_arrays中。 现在我有一个与EventListeners有关的问题。

当我从$ event-> getClientsShippings()获取数据并尝试将其传递给另一个相关实体中的“选择”时,这给了我一个错误:

Catchable Fatal Error: Object of class App\Entity\ClientsShippings could not be converted to string

我将尝试以下操作:$ shipping-> getJson()[“ clients_shippings”] [“ name”],但存在另一个错误:

Attempted to call an undefined method named "getJson" of class "Doctrine\ORM\PersistentCollection".

只有当我使用它作为函数示例时,它的工作方式是:

'choices' => function($shipping) {
return ''.$shipping->getJson()["clients_shippings"]["name"].''
}

但这会从该字段的实体而不是从eventlistener那里获取数据。

这是我的代码:

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
                ->add('client', EntityType::class, array(
    'data' => $options['client'],
    'mapped' => false,
    'attr' => ['class' => 'chosen-select','data-placeholder'=>'Wybierz klienta'],
    'class' => UserDetails::class,
    'choice_label' => function ($client) {
      if(isset($client->getJson()["client"]["firma"]))
      {
        $firma = $client->getJson()["client"]["imie"];
        }
        else {
          $firma = "";
          }
    return  ''.$firma.' '.$client->getJson()["client"]["imie"] .' '. $client->getJson()["client"]["nazwisko"].'';
      },
    'label' => 'Wybierz klienta'

                ))

        ->add('product', EntityType::class, array(
    'data' => $options['product'],
    'mapped' => false,
    'multiple' => true,
    'class' => Products::class,
    'attr' => ['class' => 'chosen-select','data-placeholder'=>'Wybierz produkt'],
    'choice_label' => function ($product) {
        return  ''.$product->getJson()["products"]["name"] .' | Stan Magazynowy: '.$product->getJson()["products"]["stock"].'';
      },
  'label' => 'Wybierz produkty'

        ))
        ->add('shipping', EntityType::class, [
          'class' => ClientsShippings::class,
          'placeholder' => 'Wybierz adres dostawy',
          'choices' => []
        ])

            ->add('save', SubmitType::class, [
            'label' => 'Zapisz',
            'attr' => ['class' => 'btn btn-primary pull-right']])
        ;


    $builder->get('client')->addEventListener(
      FormEvents::POST_SUBMIT,
      function (FormEvent $event)
      {
        $form = $event->getForm();
        $shipping = $form->getData()->getClientsShippings();

        $form->getParent()->add('shipping', EntityType::class, [
          'class' => ClientsShippings::class,
          'placeholder' => 'Wybierz adres dostawy',
          'choices' => $shipping->getJson()["clients_shippings"]["name"]


        ]);
      }
    );



    }

有什么主意如何将我从EventLitener那里获得的persistCollection传递给字段?

如果我离开“清除”数组,我从getClientsShipings()函数中获取了什么,该函数给出错误,我将尝试将数组转换为字符串。

1 个答案:

答案 0 :(得分:0)

好,解决方案很简单。我过去,如果有人会读。

  $builder->get('client')->addEventListener(
      FormEvents::POST_SUBMIT,
      function (FormEvent $event)
      {
        $form = $event->getForm();
        $shippings = $form->getData()->getClientsShippings()->getValues();

        $form->getParent()->add('shipping', EntityType::class, [
          'class' => ClientsShippings::class,
          'placeholder' => 'Wybierz adres dostawy',
          'choices' => $shippings,
          'choice_label' => function ($shipping) {
            return "".$shipping->getJson()['clients_shippings']['name']."";
          }

如果有人获得了PersistentCollection,并且需要像我一样将其转换为ArrayCollection,请使用getValues()函数,然后将该数组传递给“ choices”,然后,如果我从循环函数中读取choice_label,它将输入正确的内容。

;)