Query with json type field in Doctrine 2 and Symfony 3

Asked

Viewed 175 times

0

Does anyone know a way to perform a query using Doctrine which has a JSON field?

Example query in Postgresql:

select id, nome, tipo, '{"idUsuario":465,"login":"paulo"}'::json
from public.usuarios
limit 5;

I need to do this consultation on Doctrine 2, someone knows how?

1 answer

0

This depends somewhat on how your system is modeled. You are using the EntityManager to map their classes into tables, or not?

If so, there is a Bundle that solves this: https://github.com/boldtrn/JsonbBundle. You say that property is of the type jsonb and make the queries the way the Bundle instructs you.

If you are using DBAL, there is already support for this, as you can see in the Doctrine mapping table: http://doctrine-dbal.readthedocs.io/en/latest/reference/types.html#Mapping-Matrix

Finally, if none of the above solutions help, you can still use the function createNativeQuery to perform a function native to that particular database and have an array of results as return.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.