Pax
Pax

Reputation: 183

Symfony Doctrine Expected known function, got 'JSON_GET_TEXT'

I'm trying to use the DoctrineJsonFunctions to filter on the role column of the user table.

I have followed the documentation and searched for help but I can't find a solution for my problem.

I have installed DoctrineJsonFunctions correctly.

I have created a function in my UserRepository which is

public function findByRole(string $role)
    {
        $em = $this->getEntityManager();
        $qb = $em->createQueryBuilder();

        $qb
            ->select('u')
            ->from('App:User', 'u')
            ->where("JSON_GET_TEXT(user.roles, 'role') = :role")
            ->setParameter('role', $role)
            ->getQuery()
            ->getResult();
    }

But I'm having this error :

Expected known function, got 'JSON_GET_TEXT'

Does anyone has an idea why and can help me to solve it ?

Upvotes: 1

Views: 8063

Answers (1)

Pax
Pax

Reputation: 183

Finally, I found the reason of the error. I had to add the following in the doctrine.yaml file under dql :

JSON_GET_TEXT: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Postgresql\JsonGetText

To resume, here is my doctrine.yaml file :

doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'

        # IMPORTANT: You MUST configure your server version,
        # either here or in the DATABASE_URL env var (see .env file)
        #server_version: '13'
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App
        dql:
            string_functions:
                JSON_EXTRACT: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonExtract
                JSON_SEARCH: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonSearch
                JSON_GET_TEXT: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Postgresql\JsonGetText

Upvotes: 5

Related Questions