I use json
column in doctrine 2
(In MySQL
database).
Actually, I made my search in json
with native query like
$rsm = new ResultSetMappingBuilder($entityManager);
$rsm->addRootEntityFromClassMetadata(\blabla\MyObject::class, 'o');
$query = $entityManager->createNativeQuery('select o.* from my_objects o where json_extract(jsonData, "$.test.key1")= "value1"', $rsm);
//jsonData column contains {"test": {"key1" : "value1"}}
$result = $query->getResult();
Is it possible the made query like that without the native query mechanism ? (like a findBy)
Thanks in advance for your help ;)
I solved my problem using DQL extension scienta/doctrine-json-functions
Install it
composer req scienta/doctrine-json-functions
Usage
$queryBuilder = $entityManager->createQueryBuilder();
$query = $queryBuilder
->select("o")
->from(\bla\bla\MyObject::class, "o")
->where("JSON_EXTRACT(o.jsonData, :jsonPath) = :value ")
->setParameter('jsonPath', '$.test.key1')
->setParameter('value', 'value1')
->getQuery();
$co = $query->getResult();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With