diff --git a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php index 463a84e81..627841364 100644 --- a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php +++ b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php @@ -27,6 +27,7 @@ use Chill\MainBundle\Doctrine\DQL\GetJsonFieldByKey; use Chill\MainBundle\Doctrine\DQL\JsonAggregate; use Chill\MainBundle\Doctrine\DQL\JsonbArrayLength; use Chill\MainBundle\Doctrine\DQL\JsonbExistsInArray; +use Chill\MainBundle\Doctrine\DQL\JsonExtract; use Chill\MainBundle\Doctrine\DQL\OverlapsI; use Chill\MainBundle\Doctrine\DQL\Replace; use Chill\MainBundle\Doctrine\DQL\Similarity; @@ -231,6 +232,7 @@ class ChillMainExtension extends Extension implements 'GET_JSON_FIELD_BY_KEY' => GetJsonFieldByKey::class, 'AGGREGATE' => JsonAggregate::class, 'REPLACE' => Replace::class, + 'JSON_EXTRACT' => JsonExtract::class, ], 'numeric_functions' => [ 'JSONB_EXISTS_IN_ARRAY' => JsonbExistsInArray::class, diff --git a/src/Bundle/ChillMainBundle/Doctrine/DQL/JsonExtract.php b/src/Bundle/ChillMainBundle/Doctrine/DQL/JsonExtract.php new file mode 100644 index 000000000..9f93c437e --- /dev/null +++ b/src/Bundle/ChillMainBundle/Doctrine/DQL/JsonExtract.php @@ -0,0 +1,43 @@ +>%s', $this->element->dispatch($sqlWalker), $this->keyToExtract->dispatch($sqlWalker)); + } + + public function parse(Parser $parser) + { + $parser->match(Lexer::T_IDENTIFIER); + $parser->match(Lexer::T_OPEN_PARENTHESIS); + + $this->element = $parser->ArithmeticPrimary(); + + $parser->match(Lexer::T_COMMA); + + $this->keyToExtract = $parser->ArithmeticExpression(); + + $parser->match(Lexer::T_CLOSE_PARENTHESIS); + } +} diff --git a/src/Bundle/ChillMainBundle/Tests/Doctrine/DQL/JsonExtractTest.php b/src/Bundle/ChillMainBundle/Tests/Doctrine/DQL/JsonExtractTest.php new file mode 100644 index 000000000..3f2b3eb2f --- /dev/null +++ b/src/Bundle/ChillMainBundle/Tests/Doctrine/DQL/JsonExtractTest.php @@ -0,0 +1,52 @@ +em = self::$container->get(EntityManagerInterface::class); + } + + public function dataGenerateDql(): iterable + { + yield ['SELECT JSON_EXTRACT(c.name, \'fr\') FROM ' . Country::class . ' c', []]; + + yield ['SELECT JSON_EXTRACT(c.name, :lang) FROM ' . Country::class . ' c', ['lang' => 'fr']]; + } + + /** + * @dataProvider dataGenerateDql + */ + public function testJsonExtract(string $dql, array $args) + { + $results = $this->em->createQuery($dql) + ->setMaxResults(2) + ->setParameters($args) + ->getResult(); + + $this->assertIsArray($results, 'simply test that the query return a result'); + } +}