Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
Closed
@kriswillis

Description

@kriswillis
Issue body actions

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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.