Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i'm working with Symfony and Doctrine, i have a function that will select rows based on specific criteria:

$entities = $repository->getSomeEntities();

now i want to render those entities in a choice list, i checked the entity FormType but i couldn't achieve what i'm looking for.

Example:

     $builder->add('id','entity', array(
        'class' => 'Path\To\Entity',
        'property' => 'id'
    ));

the above code works fine except it selects all the Entities.

i checked Symfony documentation http://symfony.com/doc/current/reference/forms/types/entity.html and it seems that the only way to achieve this is by using query_builder option which wont work for my case

Is there anyway to add the $entities array to my form as a choice list ?

share|improve this question

1 Answer

See that you implement ChoiceListProvider. There are many implementation built into Symfony but you might need something as simple as SimpleChoiceListProvider.

  1. Pass EntityManager instance to form (either via constructor or options)
  2. Define form field as choice, not entity
  3. Set its choice_list to new MySimpleChoiceLIstProvider($this->entityManager)

You could (probably will) pass something more than just EntityManager to provider as you said "will select rows based on specific criteria". If that criteria origins from form itself you should probably do it via FormEvents which will give you access to data object.

Hope this helps.

share

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.