Skip to content

[Autocomplete] (Searchable Fields): Incorrect search field selection due to similar field names #2370

Closed
@HugoSEIGLE

Description

@HugoSEIGLE

Description:

In the MyAutocompleteField implementation, the searchable_fields option includes translations.name and foo.translations.name. However, when both fields share the same name (name), the autocomplete search only uses the first occurrence (translations.name). If the order is reversed, the search instead targets foo.translations.name, which leads to inconsistent search results depending on the field order.

This behavior impacts the usability of the autocomplete functionality, as it prevents the search from working properly across both related entities.

Steps to reproduce:

Define searchable_fields as ['translations.name', 'foo.translations.name'].
Trigger the autocomplete field search.
Notice that only translations.name is used in the query.
Reverse the field order to ['foo.translations.name', 'translations.name'].
Now, only foo.translations.name is considered in the search.

Expected behavior:

The autocomplete should search across both translations.name and foo.translations.name fields, regardless of their order in searchable_fields.

Actual behavior:

The search query only considers the first occurrence of name in searchable_fields, ignoring the second field.
Proposed Solution:

Consider updating the query logic to handle multiple fields with the same name explicitly. This could involve aliasing the fields or extending the searchable_fields handling to ensure both translations.name and foo.translations.name are included in the search query.

Environment:

Symfony UX Autocomplete
Symfony Form Component
PHP 8.3

Code :

namespace App\Form\AutocompleteField\MyAutocomplete;

use Override;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\ChoiceList;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\UX\Autocomplete\Form\AsEntityAutocompleteField;
use Symfony\UX\Autocomplete\Form\BaseEntityAutocompleteType;

use function sprintf;

#[AsEntityAutocompleteField(route: 'ux_entity_autocomplete')]
final class MyAutocompleteField extends AbstractType
{
    #[Override]
    public function configureOptions(OptionsResolver $optionsResolver): void
    {
        $optionsResolver
            ->setDefaults([
                'class' => MyEntity::class,
                'placeholder' => 'Choices',
                'searchable_fields' => ['translations.name', 'foo.translations.name'],
                'choice_label' => ChoiceList::label($this, $this->getChoiceLabel(...)),
                'query_builder' => ...,
            ]);
    }

    #[Override]
    public function getParent(): string
    {
        return BaseEntityAutocompleteType::class;
    }

    public function getChoiceLabel(): string
    {
        return ...;
    }
}

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