Reputation: 1695
I have two entities - Category and Article with OneToMany relation.
When I render the form for adding/editing Article object, I see all categories in an html select, but I want to see only a part of the categories (in future I would also like to add categories dinamically, according to different conditions).
How to override the query, made automatically by Doctrine, which retrieves the categories, in order to filter them?
Thanks in advance, Nikolay
Upvotes: 2
Views: 984
Reputation: 3540
Passing query_builder parameter in your category field at your ArticleType, you can customize the query for retrieve category elements.
$form = $this->createFormBuilder()
->add('category', 'entity', array(
'class' => 'ArticleBundle:Category',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('c')
->orderBy('c.name', 'ASC');
//add more query elements here
},)
)
->getForm();
View more details here
Upvotes: 5
Reputation: 8965
You can do this by setting the query_builder option to a closure that accepts the repository a an argument and returns a query builder.
Upvotes: 1