Open
Description
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;
}
}
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
loic425 commentedon May 14, 2025
cc @HeahDude any idea?
HeahDude commentedon Jun 12, 2025
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.