Closed
Description
When building a form that has a collection field that contains a form with entity fields using the query_builder
option, the query is run for each instance in the collection.
Example:
// Main form
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('participants', 'collection', array(
'type' => new ParticipantType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
));
}
// Form used in collection (ParticipantType)
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('user', 'entity', array(
'class' => 'MyUserBundle:User',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.surname', 'ASC');
},
))
->add('relationship', 'entity', array(
'class' => 'MyOtherBundle:Relationship',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('r')
->orderBy('r.name', 'ASC');
},
));
}
Now, if I generate this form with an entity that has 30 participants
, the query builder is run for both the user
and relationship
entity fields 30 times, resulting in 60 queries. Surely each query only needs to be run once?