Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Form] Query builder is run for each entity within each instance of a form within a collection #8658

Closed
@kriswillis

Description

@kriswillis

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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions