Question

I have an entity for publications with many different fields as booktitle, conference etc. I want to build a search form and one of the feature requests is to combine two search parameters in a single choice field. So far I have something like this in the form builder:

$builder->add('booktitle', 'entity', array(
              'required' => false, 
              'label' => 'Conference/Booktitle',
              'property' => 'booktitle',
              'class' => 'indPubBundle:Publication', 
              'query_builder' => function(EntityRepository $er) {
                     return $er->createQueryBuilder('p')
                               ->groupBy('p.booktitle')
                               ->orderBy('p.booktitle', 'ASC');
                      }
              ));

Basically I am displaying all booktitles as a choice field. What I want now is to have the conferences as well in the same choice field. Is there a way to achieve that?

Was it helpful?

Solution

The Entity field type is a child of the Choice field type. So you can provide Data via the "choices" parameter. Combine that with a (e.g. repository) method that returns an array with the data you need might be a solution that works for you.

$builder->add('booktitle', 'entity', array(
            'required' => false,
            'label' => 'Conference/Booktitle',
            'class' => 'indPubBundle:Publication',
            'choices' => $this->getDoctrine()->getRepository('indPubBundle:Publication')->getData(),
        ));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top