How make doctrine findby to json field without native query

I was just facing the same problem, but didn't want to install an additional extension. The code snippet from the original question just needs a small enhancement to work:

$rsm = $this->createResultSetMappingBuilder('n');
$rsm->addRootEntityFromClassMetadata(MyObject::class, 'n');
$rawQuery = sprintf('SELECT %s FROM my_objects n WHERE JSON_EXTRACT(current_state, \'$.processing\')', $rsm->generateSelectClause());
$query = $this->_em->createNativeQuery($rawQuery, $rsm);
return $query->getResult();

And it works like a charm.


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();