From 3be6fd151c7ba8dbdb33b230db2393c513cb8e77 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 6 Mar 2020 14:45:22 +0100 Subject: [PATCH] FilterProcessor: Don't utilize subquery filters if relations are joined anyway --- src/Compat/FilterProcessor.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Compat/FilterProcessor.php b/src/Compat/FilterProcessor.php index 54c087c..96ed437 100644 --- a/src/Compat/FilterProcessor.php +++ b/src/Compat/FilterProcessor.php @@ -145,6 +145,16 @@ protected function requireAndResolveFilterColumns(Filter $filter, Query $query) foreach ($filterCombinations as $column => $filters) { // The relation path must be the same for all entries $relationPath = $filters[0]->metaData['relationPath']; + + // In case the parent query also selects the relation we may not require a subquery. + // Otherwise we form a cartesian product and get unwanted results back. + $selectedByParent = isset($query->getWith()[$relationPath]); + + // Though, only single equal comparisons or those chained with an OR may be evaluated on the base + if ($selectedByParent && $sign !== '!=' && (count($filters) === 1 || $filter instanceof FilterOr)) { + continue; + } + $relation = $query->getResolver()->resolveRelation($relationPath); $subQuery = $query->createSubQuery($relation->getTarget(), $relationPath); $subQuery->columns([new Expression('1')]);