Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Form Builder - creating an array of choices from a DB query

Tags:

php

symfony

In my FormType class I have this in the buildForm method:

//...
->add('businessUnit', 'entity', array(
                'class' => 'TrainingBundle:Employee',
                'attr' => array('class' => 'form-control select2'),
                'property' => 'businessUnit',
                'empty_value' => 'All Business Units',
                'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('e')
                        ->groupBy('e.businessUnit')
                        ->orderBy('e.businessUnit', 'ASC')
                        ;
                },
                'required' => false
//...

This works fine, except instead of the "businessUnit" being put in the value of the <option> tags I get the employee ID. What I need is a dropdown with all distinct businessUnits in the Employee class. Perhaps I should be using choice instead of entity , but then I am not sure how to generate the array of choices.

ANSWER As described in the accepted answer I make this function

 private function fillBusinessUnit() {
        $er = $this->em->getRepository('TrainingBundle:Employee');

        $results = $er->createQueryBuilder('e')
               ->groupBy('e.businessUnit')
               ->orderBy('e.businessUnit', 'ASC')
               ->getQuery()
               ->getResult()
               ;

        $businessUnit = array();
        foreach($results as $bu){
             $businessUnit[$bu->getBusinessUnit()] = $bu->getBusinessUnit();
        }

        return $businessUnit;
    }

Had to pass in the EntityManager to the form. And also put use Doctrine\ORM\EntityManager; at top of the form

like image 904
Nick Barrett Avatar asked Apr 04 '14 07:04

Nick Barrett


1 Answers

Use choice instead. It has to be set with an array, so create a method to do it.

->add("type", "choice",
      array("label" => "Type",
            "choices" => $this->fillBusinessUnit(),
            "attr" => array("class" => "form-control select2"), 
            "empty_value" => 'All Business Units'))

In this method you just have to run your query with the QueryBuilder, then loop the results, fill an array and return it.

private function fillBusinessUnit() {

    $results = $er->createQueryBuilder('e')
               ->groupBy('e.businessUnit')
               ->orderBy('e.businessUnit', 'ASC');

    $businessUnit = array();
    foreach($results as $bu){
         $businessUnit[] = array("id" => $bu->getId(), "name" => $bu->getName()); // and so on..
    }

    return $businessUnit;
}

EDIT

I guess you instantiate your Type in a Controller so you can pass it in the Type construct:

$em = $this->getDoctrine()->getEntityManager();
$form = $this->createForm(new YourType($em));

then in your form class YourType.php:

class YourType extends AbstractType {

    private $em;

    public function __construct(EntityManager $em){
        $this->em = $em;
    }
}

hope this helps :)

like image 121
ponciste Avatar answered Nov 14 '22 23:11

ponciste