diff --git a/src/Compat/FilterProcessor.php b/src/Compat/FilterProcessor.php index f4f215e7..0720f568 100644 --- a/src/Compat/FilterProcessor.php +++ b/src/Compat/FilterProcessor.php @@ -233,6 +233,15 @@ protected function requireAndResolveFilterColumns( // occurrence. This is because `$generalRules` apply to all comparisons of such a column and // need to be applied to all sub queries. continue; + } elseif (isset($comparisons[Filter\Like::class])) { + // A like comparison with a value of '*' is a special case. It is used to filter for non-NULL + // values and so is semantically equivalent to a negation, not a positive comparison. Upon + // a single occurrence, all other comparisons are handled the same as for any other negation. + foreach ($comparisons[Filter\Like::class] as $rule) { + if ($rule->getValue() === '*') { + continue 2; + } + } } // Single occurring columns don't need their own sub query @@ -253,12 +262,35 @@ protected function requireAndResolveFilterColumns( $subQueryFilters = []; foreach ($columns as $column => $comparisons) { foreach ($comparisons as $conditionClass => $rules) { - if ($conditionClass === Filter\Unequal::class || $conditionClass === Filter\Unlike::class) { + if ($conditionClass === Filter\Unequal::class) { // Unequal comparisons are always put into their own sub query $subQueryFilters[] = [$rules, count($rules), true]; - } elseif (count($rules) > $count) { - // If there are multiple columns used multiple times in the same relation, we have to decide - // which to use as the primary comparison. That is the column that is used most often. + + continue; + } + + if ($conditionClass === Filter\Unlike::class) { + // An unlike comparison with a value of '*' is a special case. It is used to filter for NULL + // values and so is semantically equivalent to a positive comparison, not a negation. + $positive = []; + foreach ($comparisons[Filter\Unlike::class] as $rule) { + if ($rule->getValue() === '*') { + $positive[] = $rule; + } + } + + $rules = array_udiff($rules, $positive, fn($a, $b) => $a === $b ? 0 : -1); + $subQueryFilters[] = [$rules, count($rules), true]; + + $rules = $positive; + if (empty($rules)) { + continue; + } + } + + if (count($rules) > $count) { + // If there are multiple comparisons used many times in the same relation, we have to decide + // which to use as the primary comparison. That is the comparisons that is used most often. if (! empty($baseFilters)) { array_push($generalRules, ...$baseFilters); } diff --git a/src/Relation.php b/src/Relation.php index 179cd41c..eaf16f2f 100644 --- a/src/Relation.php +++ b/src/Relation.php @@ -563,11 +563,10 @@ public function reverse(Resolver $resolver): Relation $relation->setTarget($this->getSource()); // Propagates the same instance - if (! $this->getFilter()->isEmpty()) { - // Do not override set filters with an empty set, however, if the set is not empty - // the forward relation is expected to carry the same semantics as the inverse. - $relation->setFilter(clone $this->getFilter()); - } + // Always override the inverse relation filter with what the forward relation provides. + // This is especially important for re-used relations where the configured filter may + // have different semantics. Empty filters are not an oversight and must be propagated as well! + $relation->setFilter(clone $this->getFilter()); return $relation; } diff --git a/tests/Car.php b/tests/Car.php index d2dd5c73..ba728a23 100644 --- a/tests/Car.php +++ b/tests/Car.php @@ -36,8 +36,5 @@ public function createRelations(Relations $relations) $relations->belongsToMany('user_custom_keys', User::class) ->through(CarUserWithCustomKeys::class); - - $relations->belongsToMany('restricted_user', RestrictedUser::class) - ->through(CarUser::class); } } diff --git a/tests/Lib/Model/Employee.php b/tests/Lib/Model/Employee.php index 8f87a2f9..93876df5 100644 --- a/tests/Lib/Model/Employee.php +++ b/tests/Lib/Model/Employee.php @@ -34,6 +34,7 @@ public function createRelations(Relations $relations) { $relations->belongsTo('department', Department::class); $relations->belongsTo('office', Office::class) + ->setFilter(Filter::equal('city', 'Nuremberg')) ->setJoinType('LEFT'); $relations->hasMany('chair', Chair::class); $relations->hasMany('ticket', Ticket::class) diff --git a/tests/VisibilityFilterTest.php b/tests/VisibilityFilterTest.php index 03cdff45..83523975 100644 --- a/tests/VisibilityFilterTest.php +++ b/tests/VisibilityFilterTest.php @@ -7,7 +7,9 @@ use ipl\Stdlib\Filter; use ipl\Tests\Orm\Lib\Model\Book; use ipl\Tests\Orm\Lib\Model\Department; +use ipl\Tests\Orm\Lib\Model\Employee; use ipl\Tests\Orm\Lib\Model\Node; +use ipl\Tests\Orm\Lib\Model\Office; use ipl\Tests\Orm\Lib\Model\RestrictedGroup; use ipl\Tests\Orm\Lib\Model\RestrictedUser; use PHPUnit\Framework\TestCase; @@ -326,6 +328,29 @@ public function testModelVisibilityAndRelationFiltersAreAppliedInAFilterSubQuery ); } + public function testRelationFiltersAreOverriddenByEmptyRulesInReversedSubQueries() + { + // Filtering on a to-many relation is turned into a reversed sub query by the FilterProcessor. + // The sub query's base (employee) must carry both its own visibility filter (deleted = n) but + // not the relation filter declared on the inverse relation (city = Nuremberg) because the forward + // relation has no filter. + $query = (new Query()) + ->setDb(new TestConnection()) + ->setModel(new Office()) + ->columns('city') + ->filter(Filter::equal('employee.name', 'John Doe')); + + $this->assertSql( + 'SELECT office.city FROM office WHERE office.id IN ((SELECT' + . ' sub_employee_office.id AS sub_employee_office_id FROM employee sub_employee' + . ' LEFT JOIN office sub_employee_office' + . ' ON sub_employee_office.id = sub_employee.office_id' + . ' WHERE (sub_employee.deleted = ?) AND (sub_employee.name = ?)))', + $query->assembleSelect(), + ['n', 'John Doe'] + ); + } + public function testFiltersArePropagatedThroughTheReversedJoinsOfASubQuery() { // Two-hop path: the sub query's base is the final target (ticket) and the intermediate model