diff --git a/src/Bridge/Doctrine/ORM/EventRepository.php b/src/Bridge/Doctrine/ORM/EventRepository.php index 9f6f37d..b8ded58 100644 --- a/src/Bridge/Doctrine/ORM/EventRepository.php +++ b/src/Bridge/Doctrine/ORM/EventRepository.php @@ -13,7 +13,6 @@ use CalendR\Event\EventInterface; use Doctrine\ORM\AbstractQuery; -use Doctrine\ORM\Query; use Doctrine\ORM\QueryBuilder; /** @@ -28,31 +27,18 @@ public function getEventsQueryBuilder(\DateTimeInterface $begin, \DateTimeInterf { $qb = $this->createQueryBuilderForGetEvent($options); + $beginField = $this->getBeginFieldName(); + $endField = $this->getEndFieldName(); + return $qb ->andWhere( - $qb->expr()->orX( - // Period in event - $qb->expr()->andX( - $qb->expr()->lte($this->getBeginFieldName(), $begin), - $qb->expr()->gte($this->getEndFieldName(), $end) - ), - // Event in period - $qb->expr()->andX( - $qb->expr()->gte($this->getBeginFieldName(), $begin), - $qb->expr()->lt($this->getEndFieldName(), $end) - ), - // Event begins during period - $qb->expr()->andX( - $qb->expr()->lt($this->getBeginFieldName(), $end), - $qb->expr()->gte($this->getBeginFieldName(), $begin) - ), - // Event ends during period - $qb->expr()->andX( - $qb->expr()->gte($this->getEndFieldName(), $begin), - $qb->expr()->lt($this->getEndFieldName(), $end) - ) + $qb->expr()->andX( + "${beginField} < :end", + "${endField} > :begin" ) - ); + ) + ->setParameter(':begin', $begin) + ->setParameter(':end', $end); } public function getEventsQuery(\DateTimeInterface $begin, \DateTimeInterface $end, array $options = []): AbstractQuery diff --git a/src/Bridge/Symfony/Bundle/Resources/config/services.yaml b/src/Bridge/Symfony/Bundle/Resources/config/services.yaml index ee8094b..6ba3813 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/services.yaml +++ b/src/Bridge/Symfony/Bundle/Resources/config/services.yaml @@ -6,11 +6,9 @@ services: CalendR\Event\Manager: class: 'CalendR\Event\Manager' - CalendR\Extension\Twig\CalendRExtension: - class: 'CalendR\Extension\Twig\CalendRExtension' - arguments: [ '@calendr' ] - tags: - - { name: twig.extension } + CalendR\Bridge\Twig\CalendRExtension: + autowire: true + autoconfigure: true calendr: alias: 'CalendR\Calendar' diff --git a/tests/Bridge/Doctrine/ORM/EventRepositoryTest.php b/tests/Bridge/Doctrine/ORM/EventRepositoryTest.php index 4d2f1fe..37f01c7 100644 --- a/tests/Bridge/Doctrine/ORM/EventRepositoryTest.php +++ b/tests/Bridge/Doctrine/ORM/EventRepositoryTest.php @@ -62,9 +62,9 @@ public function testGetEvents($begin, $end, array $providedEvents): void $this->qb->expects($this->once())->method('from')->willReturn($this->qb); $this->qb->expects($this->once())->method('andWhere')->willReturn($this->qb); $this->qb->expects($this->once())->method('getQuery')->willReturn($query); - $this->qb->expects($this->atLeastOnce())->method('expr')->willReturn($expr); - $expr->expects($this->once())->method('orX'); - $expr->expects($this->exactly(4))->method('andX'); + $this->qb->expects($this->exactly(2))->method('setParameter')->willReturn($this->qb); + $this->qb->expects($this->once())->method('expr')->willReturn($expr); + $expr->expects($this->once())->method('andX'); $events = $this->repo->getEvents(new \DateTimeImmutable($begin), new \DateTimeImmutable($end)); $this->assertSame($providedEvents, $events);