Skip to content

Commit

Permalink
Searching by related entities fields
Browse files Browse the repository at this point in the history
While we can make add columns of an related entity in the "list" actions, there is no way to search by them.

```yml
easy_admin:
    entities:
        Campaign:
            list:
                fields:
                    - property: name
                    - property: domain.name // <---
```


```yml
easy_admin:
    entities:
        Campaign:
            search:
                fields:
                    - domain.name  // <--- throwing error
```

These changes are making that it will now be possible.
What do you think about it?
  • Loading branch information
Krzysztof Trzos committed Nov 6, 2017
1 parent e8af474 commit 984018f
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Search/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ public function createSearchQueryBuilder(array $entityConfig, $searchQuery, $sor

$queryParameters = array();
foreach ($entityConfig['search']['fields'] as $name => $metadata) {
$fieldPrefix = 'entity';

if(strpos($name, '.') !== false) {
[$fieldPrefix, $name] = explode('.', $name);
$queryBuilder->join('entity.'.$fieldPrefix, $fieldPrefix);
}

$isSmallIntegerField = 'smallint' === $metadata['dataType'];
$isIntegerField = 'integer' === $metadata['dataType'];
$isNumericField = in_array($metadata['dataType'], array('number', 'bigint', 'decimal', 'float'));
Expand All @@ -114,17 +121,17 @@ public function createSearchQueryBuilder(array $entityConfig, $searchQuery, $sor
$isIntegerField && $isSearchQueryInteger ||
$isNumericField && $isSearchQueryNumeric
) {
$queryBuilder->orWhere(sprintf('entity.%s = :numeric_query', $name));
$queryBuilder->orWhere(sprintf('%s.%s = :numeric_query', $fieldPrefix, $name));
// adding '0' turns the string into a numeric value
$queryParameters['numeric_query'] = 0 + $searchQuery;
} elseif ($isGuidField && $isSearchQueryUuid) {
$queryBuilder->orWhere(sprintf('entity.%s = :uuid_query', $name));
$queryBuilder->orWhere(sprintf('%s.%s = :uuid_query', $fieldPrefix, $name));
$queryParameters['uuid_query'] = $searchQuery;
} elseif ($isTextField) {
$queryBuilder->orWhere(sprintf('LOWER(entity.%s) LIKE :fuzzy_query', $name));
$queryBuilder->orWhere(sprintf('LOWER(%s.%s) LIKE :fuzzy_query', $fieldPrefix, $name));
$queryParameters['fuzzy_query'] = '%'.$lowerSearchQuery.'%';

$queryBuilder->orWhere(sprintf('LOWER(entity.%s) IN (:words_query)', $name));
$queryBuilder->orWhere(sprintf('LOWER(%s.%s) IN (:words_query)', $fieldPrefix, $name));
$queryParameters['words_query'] = explode(' ', $lowerSearchQuery);
}
}
Expand Down

0 comments on commit 984018f

Please sign in to comment.