EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

[SOLVED] Erroneously fails to search when table names match

Open ksn135 opened this issue 8 months ago • 0 comments

Describe the bug In the Document object I have a mapping to the counterparties table (field contractor) and a related table with additional counterparties (field contractors), which also refers to the counterparties table contractor.

In the search settings, I set the following parameters:

    public function configureCrud(Crud $crud): Crud
    {
        return $crud
           // ....
            ->setSearchFields([
                // ....
                'contractor.title', 'contractor.inn', 'contractor.ogrn',
                'contractors.contractor.title', 'contractors.contractor.inn', 'contractors.contractor.ogrn'
            ])
    }

The error is that the alias of the table is formed incorrectly in the generated query. As it coincides with the name of the counterparties table (contractor). That is, instead of searching in the entity.contractors.contractor.title field, only the entity.contractor.title field is searched. Screenshot 2024-06-16 at 15 26 43 And it should be like this: Screenshot 2024-06-16 at 15 26 19

Examples We're trying to locate an additional contractor named Mr. Terry Johns IV. Screenshot 2024-06-16 at 15 44 36 Oops! Nothing found! Screenshot 2024-06-16 at 15 45 57 It was supposed to find it and show it like this Screenshot 2024-06-16 at 15 44 59

Solution

diff --git a/src/Orm/EntityRepository.php b/src/Orm/EntityRepository.php
index 042dd088..74a28569 100644
--- a/src/Orm/EntityRepository.php
+++ b/src/Orm/EntityRepository.php
@@ -262,13 +262,13 @@ final class EntityRepository implements EntityRepositoryInterface
                 $associatedEntityAlias = $associatedPropertyName = '';
                 for ($i = 0; $i < $numAssociatedProperties - 1; ++$i) {
                     $associatedEntityName = $associatedProperties[$i];
-                    $associatedEntityAlias = Escaper::escapeDqlAlias($associatedEntityName);
+                    $associatedEntityAlias = Escaper::escapeDqlAlias($associatedEntityName) . ($i ?: '');
                     $associatedPropertyName = $associatedProperties[$i + 1];
 
-                    if (!\in_array($associatedEntityName, $entitiesAlreadyJoined, true)) {
+                    if (!\in_array($associatedEntityAlias, $entitiesAlreadyJoined, true)) {
                         $parentEntityName = 0 === $i ? 'entity' : $associatedProperties[$i - 1];
                         $queryBuilder->leftJoin(Escaper::escapeDqlAlias($parentEntityName).'.'.$associatedEntityName, $associatedEntityAlias);
-                        $entitiesAlreadyJoined[] = $associatedEntityName;
+                        $entitiesAlreadyJoined[] = $associatedEntityAlias;
                     }
 
                     if ($i < $numAssociatedProperties - 2) {

ksn135 avatar Jun 16 '24 14:06 ksn135