How to show a form in Symfony from a query to 2 entities

0

I have the following code that works:

//AdminController.php
$orders_repo=$em->getRepository("BackendBundle:Orders");
$order=$orders_repo->getQuery()->getResult();
$form = $this->createForm(OrderType::class, $order);

And in the OrderType form this code:

$builder
        ->add('customername', TextType::class, array(
                'label'=>'Numero de Causa',
                'required'=>'Requerido',
                'attr'=> array (
                    'class' => 'form-name form-control'
                )
            ));

The problem is when you want to show a more elaborate query to show in the form.

Something like this:

$query = $em->createQuery(
'SELECT u, o
                FROM BackendBundle:Orders o
                JOIN o.users u
    where o.orderid = :orderid'
 )->setParameter('orderid', $id);

 $order = $query->getResult();
 $form = $this->createForm(OrderType::class, $order);

If you want to show this in the form and then update how it can be done?

    
asked by juanitourquiza 03.08.2017 в 03:17
source

1 answer

1

In this case you should use EntityType , previously with the entity User related to your entity Orders .

Staying something like the following:

$builder
        ->add('customername', TextType::class, array(
                'label'=>'Numero de Causa',
                'required'=>'Requerido',
                'attr'=> array (
                    'class' => 'form-name form-control'
                )
            ));
        ->add('user', EntityType::class, array(
              'class' => 'BackendBundle:User',
              'placeholder' => 'Escoge un usuario...',
              'choice_label' => 'nombre',
              'query_builder' => function(\BackendBundle\Entity\User $er) {
                     return $er->getUsersComoQuieras();
              }
        ));

EntityType normally generates a select in which you can choose the entities (users in your case) within the form. You can check the EntityType documentation , as you'll see in the example using query_builder it is optional, you can use it if you want the select to have specific users according to your query or not use it to get you all out. The fields choice_value and choice_label are to tell you where you will get the value and the label that will be displayed in each option of the select.

    
answered by 03.08.2017 в 08:46