EasyAdminBundle
EasyAdminBundle copied to clipboard
[SOLVED] Erroneously fails to search when table names match
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.
And it should be like this:
Examples
We're trying to locate an additional contractor named Mr. Terry Johns IV
.
Oops! Nothing found!
It was supposed to find it and show it like this
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) {