diff --git a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php index 463a84e81..f1fe4c44d 100644 --- a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php +++ b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php @@ -22,6 +22,7 @@ use Chill\MainBundle\Controller\UserController; use Chill\MainBundle\Controller\UserJobApiController; use Chill\MainBundle\Controller\UserJobController; use Chill\MainBundle\DependencyInjection\Widget\Factory\WidgetFactoryInterface; +use Chill\MainBundle\Doctrine\DQL\Age; use Chill\MainBundle\Doctrine\DQL\Extract; use Chill\MainBundle\Doctrine\DQL\GetJsonFieldByKey; use Chill\MainBundle\Doctrine\DQL\JsonAggregate; @@ -243,6 +244,7 @@ class ChillMainExtension extends Extension implements 'datetime_functions' => [ 'EXTRACT' => Extract::class, 'TO_CHAR' => ToChar::class, + 'AGE' => Age::class, ], ], 'hydrators' => [ diff --git a/src/Bundle/ChillMainBundle/Doctrine/DQL/Age.php b/src/Bundle/ChillMainBundle/Doctrine/DQL/Age.php new file mode 100644 index 000000000..ad148fe99 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Doctrine/DQL/Age.php @@ -0,0 +1,54 @@ +value2) { + return sprintf( + 'AGE(%s, %s)', + $this->value1->dispatch($sqlWalker), + $this->value2->dispatch($sqlWalker) + ); + } + + return sprintf( + 'AGE(%s)', + $this->value1->dispatch($sqlWalker), + ); + } + + public function parse(Parser $parser) + { + $parser->match(Lexer::T_IDENTIFIER); + $parser->match(Lexer::T_OPEN_PARENTHESIS); + + $this->value1 = $parser->SimpleArithmeticExpression(); + + $parser->match(Lexer::T_COMMA); + + $this->value2 = $parser->SimpleArithmeticExpression(); + + $parser->match(Lexer::T_CLOSE_PARENTHESIS); + } +} diff --git a/src/Bundle/ChillMainBundle/Tests/Doctrine/DQL/AgeTest.php b/src/Bundle/ChillMainBundle/Tests/Doctrine/DQL/AgeTest.php new file mode 100644 index 000000000..90f6a24bf --- /dev/null +++ b/src/Bundle/ChillMainBundle/Tests/Doctrine/DQL/AgeTest.php @@ -0,0 +1,79 @@ +entityManager = self::$container->get(EntityManagerInterface::class); + } + + public function generateQueries(): iterable + { + yield [ + 'SELECT AGE(a.validFrom, a.validTo) FROM ' . Address::class . ' a', + [], + ]; + + yield [ + 'SELECT AGE(:date0, :date1) FROM ' . Address::class . ' a', + [ + 'date0' => new DateTimeImmutable('now'), + 'date1' => new DateTimeImmutable('2020-01-01'), + ], + ]; + + yield [ + 'SELECT AGE(a.validFrom, :date1) FROM ' . Address::class . ' a', + [ + 'date1' => new DateTimeImmutable('now'), + ], + ]; + + yield [ + 'SELECT AGE(:date0, a.validFrom) FROM ' . Address::class . ' a', + [ + 'date0' => new DateTimeImmutable('now'), + ], + ]; + } + + /** + * @dataProvider generateQueries + */ + public function testWorking(string $dql, array $args) + { + $dql = $this->entityManager->createQuery($dql)->setMaxResults(3); + + foreach ($args as $key => $value) { + $dql->setParameter($key, $value); + } + + $results = $dql->getResult(); + + $this->assertIsArray($results); + } +}