diff --git a/src/Filter/AbstractDateFilter.php b/src/Filter/AbstractDateFilter.php index c470ef13f..fc6951dac 100644 --- a/src/Filter/AbstractDateFilter.php +++ b/src/Filter/AbstractDateFilter.php @@ -160,7 +160,13 @@ private function filterRange(ProxyQueryInterface $query, string $alias, string $ $endDateParameterName = $this->getNewParameterName($query); if (DateRangeOperatorType::TYPE_NOT_BETWEEN === $type) { - $this->applyWhere($query, sprintf('%s.%s < :%s OR %s.%s > :%s', $alias, $field, $startDateParameterName, $alias, $field, $endDateParameterName)); + if (null !== $value['start']) { + $this->applyWhere($query, sprintf('%s.%s %s :%s', $alias, $field, '<', $startDateParameterName)); + } + + if (null !== $value['end']) { + $this->applyWhere($query, sprintf('%s.%s %s :%s', $alias, $field, '>', $endDateParameterName)); + } } else { if (null !== $value['start']) { $this->applyWhere($query, sprintf('%s.%s %s :%s', $alias, $field, '>=', $startDateParameterName)); diff --git a/tests/Filter/DateTimeRangeFilterTest.php b/tests/Filter/DateTimeRangeFilterTest.php index 53dafa97a..784128669 100644 --- a/tests/Filter/DateTimeRangeFilterTest.php +++ b/tests/Filter/DateTimeRangeFilterTest.php @@ -14,6 +14,7 @@ namespace Sonata\DoctrineORMAdminBundle\Tests\Filter; use Sonata\AdminBundle\Filter\Model\FilterData; +use Sonata\AdminBundle\Form\Type\Operator\DateRangeOperatorType; use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery; use Sonata\DoctrineORMAdminBundle\Filter\DateTimeRangeFilter; use Sonata\Form\Type\DateTimeRangeType; @@ -43,4 +44,48 @@ public function testGetType(): void static::assertSame(DateTimeRangeType::class, $filter->getFieldType()); } + + public function testFilterNotBetweenStartDate(): void + { + $filter = new DateTimeRangeFilter(); + $filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); + + $proxyQuery = new ProxyQuery($this->createQueryBuilderStub()); + + $startDateTime = new \DateTime('2023-10-03T12:00:01'); + + $filter->filter($proxyQuery, 'alias', 'field', FilterData::fromArray([ + 'type' => DateRangeOperatorType::TYPE_NOT_BETWEEN, + 'value' => [ + 'start' => $startDateTime, + 'end' => null, + ], + ])); + + self::assertSameQuery(['WHERE alias.field < :field_name_0'], $proxyQuery); + self::assertSameQueryParameters(['field_name_0' => $startDateTime], $proxyQuery); + static::assertTrue($filter->isActive()); + } + + public function testFilterNotBetweenEndDate(): void + { + $filter = new DateTimeRangeFilter(); + $filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); + + $proxyQuery = new ProxyQuery($this->createQueryBuilderStub()); + + $endDateTime = new \DateTime('2023-10-03T12:00:01'); + + $filter->filter($proxyQuery, 'alias', 'field', FilterData::fromArray([ + 'type' => DateRangeOperatorType::TYPE_NOT_BETWEEN, + 'value' => [ + 'start' => null, + 'end' => $endDateTime, + ], + ])); + + self::assertSameQuery(['WHERE alias.field > :field_name_1'], $proxyQuery); + self::assertSameQueryParameters(['field_name_1' => $endDateTime], $proxyQuery); + static::assertTrue($filter->isActive()); + } }