From caf8b7bd5ab71fd009459fa34f5ddd19c0139e56 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 22 Feb 2024 16:50:14 +0100 Subject: [PATCH] wip --- src/Compat/FilterProcessor.php | 7 +++++-- tests/FilterProcessorTest.php | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/Compat/FilterProcessor.php b/src/Compat/FilterProcessor.php index 7956898..0759c24 100644 --- a/src/Compat/FilterProcessor.php +++ b/src/Compat/FilterProcessor.php @@ -258,8 +258,11 @@ protected function requireAndResolveFilterColumns(Filter\Rule $filter, Query $qu } } - if (! empty($baseFilters) || ! empty($generalRules)) { - $subQueryFilters[] = [$baseFilters ?: $generalRules, $count, false]; + if (! empty($baseFilters)) { + $subQueryFilters[] = [$baseFilters, $count, false]; + } elseif (! empty($generalRules) && empty($subQueryFilters)) { + $subQueryFilters[] = [$generalRules, null, false]; + $generalRules = []; } foreach ($subQueryFilters as list($filters, $count, $negate)) { diff --git a/tests/FilterProcessorTest.php b/tests/FilterProcessorTest.php index c80c0c1..a3b56df 100644 --- a/tests/FilterProcessorTest.php +++ b/tests/FilterProcessorTest.php @@ -4,10 +4,14 @@ use ipl\Orm\Compat\FilterProcessor; use ipl\Orm\Query; +use ipl\Sql\Connection; use ipl\Stdlib\Filter; +use ipl\Tests\Orm\Lib\DataProviders; class FilterProcessorTest extends \PHPUnit\Framework\TestCase { + use DataProviders; + public function testUnequalDoesNotOverrideUnlike() { $query = new Query(); @@ -62,4 +66,38 @@ public function testUnequalDoesNotOverrideUnlike() $where[1][0][1][1][1]['(car.id NOT IN (?) OR car.id IS NULL)']->getWhere() ); } + + /** + * @dataProvider databases + */ + public function testUnequalTargetingAToManyRelationProducesResults(Connection $db) + { + $db->insert('car', ['id' => 1, 'model_name' => 'foo', 'manufacturer' => 'bar']); + $db->insert('passenger', ['id' => 1, 'car_id' => 1, 'name' => 'foo', 'sex' => 'bar']); + $db->insert('passenger', ['id' => 2, 'car_id' => 1, 'name' => 'bar', 'sex' => 'foo']); + $db->insert('car', ['id' => 2, 'model_name' => 'bar', 'manufacturer' => 'foo']); + $db->insert('passenger', ['id' => 3, 'car_id' => 2, 'name' => 'foo', 'sex' => 'oof']); + + $car = Car::on($db); + $car->columns(['car.model_name']); + $car->filter(Filter::all( + Filter::equal('passenger.name', 'bar'), + Filter::unequal('passenger.sex', 'foo') + )); + + $this->assertSame(1, $car->count()); + $this->assertSame('bar', $car->first()->model_name); + } + + protected function createSchema(Connection $db, string $driver): void + { + $db->exec('CREATE TABLE car (id INT PRIMARY KEY, model_name VARCHAR(255), manufacturer VARCHAR(255))'); + $db->exec('CREATE TABLE passenger (id INT PRIMARY KEY, car_id INT, name VARCHAR(255), sex VARCHAR(255))'); + } + + protected function dropSchema(Connection $db, string $driver): void + { + $db->exec('DROP TABLE IF EXISTS passenger'); + $db->exec('DROP TABLE IF EXISTS car'); + } }