Skip to content

[UxAutocomplete] Extra Doctrine query thrown when defining a choice_value. #2736

Open
@loic425

Description

@loic425

When defining a choice_value, an extra Doctrine query is thrown without any where clause.
This could ruin performance of your application.

Maybe this is not due to the ux component but on the form component itself?

Of course, on my example, we just need to remove choice_value, because the id is the default. But we have this issue when the value is used with a slug property.

namespace App\Form;

use App\Entity\Author;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\UX\Autocomplete\Form\AsEntityAutocompleteField;
use Symfony\UX\Autocomplete\Form\BaseEntityAutocompleteType;

#[AsEntityAutocompleteField]
class AuthorAutocompleteField extends AbstractType
{
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'class' => Author::class,
            'placeholder' => 'What should we eat?',
            'choice_value' => 'id',
            'choice_label' => 'name',
        ]);
    }

    public function getParent(): string
    {
        return BaseEntityAutocompleteType::class;
    }
}

Image

Activity

loic425

loic425 commented on May 14, 2025

@loic425
Author

cc @HeahDude any idea?

HeahDude

HeahDude commented on Jun 12, 2025

@HeahDude

The id as choice value is the default and let Symfony handle query optimization.

When an explicit choice value is problematic performance wise, you should define a choice loader instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @loic425@HeahDude@carsonbot

        Issue actions

          [UxAutocomplete] Extra Doctrine query thrown when defining a choice_value. · Issue #2736 · symfony/ux