Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
HugoSEIGLE opened this issue Nov 13, 2024 · 3 comments · May be fixed by #2377
Open
Labels

Comments

@HugoSEIGLE
Copy link

HugoSEIGLE commented Nov 13, 2024

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 ...;
    }
}
@HugoSEIGLE HugoSEIGLE added the Bug Bug Fix label Nov 13, 2024
@HugoSEIGLE HugoSEIGLE changed the title [Autocomplete] (Searchable Fields): Incorrect Search Field Selection in Autocomplete Due to Similar Field Names [Autocomplete] (Searchable Fields): Incorrect search field selection due to similar field names Nov 13, 2024
@smnandre
Copy link
Member

Hi @HugoSEIGLE! Thank you for this report... with such a vision on the problem and where it occurs... would you maybe have time to submit a fix ? :)

@smnandre smnandre removed the Status: Needs Review Needs to be reviewed label Nov 14, 2024
@HugoSEIGLE
Copy link
Author

Hi @HugoSEIGLE! Thank you for this report... with such a vision on the problem and where it occurs... would you maybe have time to submit a fix ? :)

Hi, it's done :)

@smnandre
Copy link
Member

Well.. thank you very much! 😄

(i will look at it after the week-end)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants