Skip to content

Commit

Permalink
feature #2424 Add support for searching by multiple nested associatio…
Browse files Browse the repository at this point in the history
…ns (javiereguiluz)

This PR was merged into the 1.x branch.

Discussion
----------

Add support for searching by multiple nested associations

This implements the feature proposed by @ucay in #2406.

Commits
-------

5047dff Add support for searching by multiple nested associations
  • Loading branch information
javiereguiluz committed Nov 21, 2018
2 parents f2b75c5 + 5047dff commit a96c87d
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/Search/QueryBuilder.php
Expand Up @@ -104,14 +104,21 @@ public function createSearchQueryBuilder(array $entityConfig, $searchQuery, $sor
foreach ($entityConfig['search']['fields'] as $fieldName => $metadata) {
$entityName = 'entity';
if ($this->isDoctrineAssociation($classMetadata, $fieldName)) {
list($associatedEntityName, $associatedFieldName) = explode('.', $fieldName);
if (!in_array($associatedEntityName, $entitiesAlreadyJoined)) {
$queryBuilder->leftJoin('entity.'.$associatedEntityName, $associatedEntityName);
$entitiesAlreadyJoined[] = $associatedEntityName;
// support arbitrarily nested associations (e.g. foo.bar.baz.qux)
$associationComponents = explode('.', $fieldName);
for ($i = 0; $i < count($associationComponents) - 1; ++$i) {
$associatedEntityName = $associationComponents[$i];
$associatedFieldName = $associationComponents[$i + 1];

if (!in_array($associatedEntityName, $entitiesAlreadyJoined)) {
$parentEntityName = 0 === $i ? 'entity' : $associationComponents[$i - 1];
$queryBuilder->leftJoin($parentEntityName.'.'.$associatedEntityName, $associatedEntityName);
$entitiesAlreadyJoined[] = $associatedEntityName;
}

$entityName = $associatedEntityName;
$fieldName = $associatedFieldName;
}

$entityName = $associatedEntityName;
$fieldName = $associatedFieldName;
}

$isSmallIntegerField = 'smallint' === $metadata['dataType'];
Expand Down

0 comments on commit a96c87d

Please sign in to comment.