I solved it as follows:
in FormType should be the same:
->add('preferred_language', 'entity', array( 'mapped' => false, 'property' => 'name', 'class' => 'Common\MainBundle\Entity\Language', 'query_builder' => function(\Doctrine\ORM\EntityRepository $er) { return $er->createQueryBuilder('u') ->orderBy('u.id', 'DESC'); }
In the controller, I get data using DQL:
$em = $this->getDoctrine()->getManager(); $query = $em->createQuery('SELECT u FROM MyBundle:Language ORDER BY u.id DESC'); $data = $query->getResult();
I pass data using the render method:
return $this->render('MyBundle:Default:page.html.twig', array('formulario' => $formulario->createView(), 'data' => $data));
In the twig file, I create a <select>
element with the identifier "myproject_mybundle_myformtype_ preferred_language ":
<select id="myproject_mybundle_myformtype_preferred_language" name="aeneagrama_adminbundle_itemcontenidotype[preferred_language]" class="form-control"> <option value="0">-- Choose an option --</option> {% for item in data %} <option value="{{ item.your_column }}">{{ item.name }}</option> {% endfor %}</select>
Finally, when you get input from the form, you can get it in the controller:
$form->get('preferred_language')->getData();
source share