7

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

1 Answer 1

8

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 :)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this is on the right track I think. How would I get the $er variable though?
Thanks! I had to make a few changes but it works now :) (Put the changes in the original question)
great, glad it worked. Yes sorry i wrote the code right here and I couldn't test it. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.