PersisCollection in EntityType Form

拈花ヽ惹草 提交于 2019-12-11 16:54:51

问题


I am saving all of data from forms to JSON_arrays in DB. Now i got a problem with EventListeners.

When i taking a data from $event->getClientsShippings() and try to pass it to 'choices' in another related Entity this gives me a error that:

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

I`ll try this: $shipping->getJson()["clients_shippings"]["name"] but there is another error that:

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

Only way that`s working is if i use it as function example:

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

But this tooks data from entity of this field not from eventlistener.

Here is my code:

    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"]


        ]);
      }
    );



    }

Any idea how i can pass persistCollection what i got from EventLitener to field ?

If i leave "clear" array what i took from getClientsShipings() function that gives error that i`ll try to convert array to string.


回答1:


Ok the solution is easy. I past if someone will read it.

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

If someone got PersistentCollection and need to convert it to ArrayCollection as I, use getValues() function, and then I passed this array to 'choices' and then if i readed choice_label from loop function it took correct entrys.

;)



来源:https://stackoverflow.com/questions/53175644/persiscollection-in-entitytype-form

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!