How make doctrine findby to json field without native query

后端 未结 2 640
无人及你
无人及你 2020-12-17 22:02

I use json column in doctrine 2 (In MySQL database). Actually, I made my search in json with native query like

         


        
相关标签:
2条回答
  • 2020-12-17 22:39

    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.

    0 讨论(0)
  • 2020-12-17 22:48

    I solved my problem. I found this extension of DQL .

    $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();
    
    0 讨论(0)
提交回复
热议问题