H
H
HellWalk2020-02-21 12:37:18
PostgreSQL
HellWalk, 2020-02-21 12:37:18

Doctrine + Postgres - how to build a QueryBuilder with JSON search?

There is a working SQL query:

SELECT * FROM "user" WHERE "roles"::text = '["ROLE_COURIER"]';


It is necessary to compose it on QueryBuilder.

If you write like this:
$role = '["ROLE_COURIER"]';

        return $this->createQueryBuilder('user')
            ->where('user.roles = :role')
            ->setParameter('role', $role)
            ->getQuery()
            ->getResult()
            ;

Gives an error message:
SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: json = unknown


If you write like this:
$role = '["ROLE_COURIER"]';

        return $this->createQueryBuilder('user')
            ->where('"roles"::text = :role')
            ->setParameter('role', $role)
            ->getQuery()
            ->getResult()
            ;

Gives an error message:
[Syntax Error] line 0, col 44: Error: Expected Literal, got '"'


I can’t figure it out - so how to correctly make a request with a condition on the json field?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Derepko, 2020-02-21
@uDenX

for example

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question