EntityType 形式的 PersisCollection

PersisCollection in EntityType Form

我正在将表单中的所有数据保存到数据库中的 JSON_arrays。 现在我遇到了 EventListeners 的问题。

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

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"].''
}

但这从该字段的实体中获取数据,而不是从事件监听器中获取数据。

这是我的代码:

    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 传递给字段吗?

如果我离开 "clear" 数组,我从 getClientsShipings() 函数中获取的内容会给出错误,我将尝试将数组转换为字符串。

好的,解决方法很简单。如果有人会读,我就过去了。

  $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它采取了正确的条目。

;)