E
E
e-hot2015-12-17 15:47:59
symfony
e-hot, 2015-12-17 15:47:59

How to implement the transfer of entity objects to a form with multiple selection?

Welcome all. Need help in the following situation:
0. Task: editing the list of cities assigned to a specific user. (We don't edit the user.)
1. There is a DB table Usercity with two fields user_id and city_id - n number of cities can be assigned to a user ( 1:N ). There is a corresponding Entity.
2. There is a Cities database table - this is a reference book with the entire initial list of cities in the system. There is a corresponding Entity.
3. A form is written through the form class to display a list of the entire list of cities with multiple choice (it doesn’t matter here: it will be select or checkboxes), in which (in the list) in accordance with the city_ids selected from Usercity for the specified user_id are marked as selected.
Question:I can’t figure out how to pass data from Usercity to the form class in the object format, because Symfony yells - requires data as objects, not string data. To illustrate the experience below:
Form class:

public function buildForm(FormBuilderInterface $builder, array $options)
{
        $builder->add( 'user_id' , 'hidden' )
                ->add('city_id', 'entity', array( 
                                                'class' => 'Acme\AppBundle\Entity\User\Cities',
                                                'query_builder' => function ( EntityRepository $er ) {
                                                                                return $er->createQueryBuilder( 'u' )
                                                                                          ->groupBy( 'u.city_name' )
                                                                                          ->orderBy( 'u.city_name', 'ASC' );
                                                                    },
                                                'property' => 'city_name',
                                                'preferred_choices' => $this->getCities(),
                                                'multiple' => true,
                                                'label' => false,
                                                'required'  => true,
                                            ) 
                     )
                ->add( 'save', 'submit', array( 'label' => 'Сохранить' ) );
}

Controller:
public function editCitiesAction( $user_id, Request $request )
{
        $em = $this->getDoctrine()->getManager();
        $cities = $em->getRepository('Acme\AppBundle\Entity\User\Usercity')->findBy( array( 'user_id' => $user_id ) );
        
        $form = $this->createForm( new editCitiesType( ) ); 

        ...

        return $this->render('AcmeAppBundle:Forms:editCities.html.twig', array( 'form' => $form->createView() ) );
}

Clarification of the question: how to pass $cities to the form class?
Thanks in advance for your help.
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis, 2015-12-18
@prototype_denis

Who owns the cities? To the user... Well and give the user.

// controller
$user = $repo->find($user_id);
$this->createForm(new UserCityType(), $user);


//  buildForm 
$builder->add('cities');

Everything.

E
e-hot, 2015-12-18
@e-hot

Thanks for the answer. For some reason, the standard solution did not work out in my case (I also experimented with collection ), so I had to solve the problem in a different way - by passing two arrays to the form class: one is the complete list of cities, the second is the list of cities of the specified user. In the form class, these two arrays are specified in the field settings of the choice type, where:

'choices' => array( полный перечень городов ),
'preferred_choices' => array( перечень городов заданного юзера ),
'multiple' => true,

More than 3,000 cities are loaded into the created list with multiple choice rather quickly.
All - thank you all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question