Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed Feb 22, 2024
1 parent c536cce commit caf8b7b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Compat/FilterProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
38 changes: 38 additions & 0 deletions tests/FilterProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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');
}
}

0 comments on commit caf8b7b

Please sign in to comment.