Request on key/value in a JSON type field with Doctrine2

arnaudbey picture arnaudbey · Mar 14, 2018 · Viewed 9.9k times · Source

I'm trying to figure out how, in a symfony 3.4 app, to retrieve (through a repository method, with a DQL request for example) entities depending on a value for a specific key in a "json" typed column. Saw there's some stuff possible with postgre but I didnt find anything with mariaDB

Let's say I get an entity Letter

with this property :

/**
 *
 * @ORM\Column(type="json")
 */
private $metadatas;

which contains, for example:

 {
    "key1": "value",
    "key2": "value"
 }

How can I, or, Is it possible to request my DB to get letters with a specific value for a specific key in metadatas column.

Something like that :

public function getByKeyValue($key, $value)
      {
          $em = $this->_em;
          $dql = "SELECT l FROM AppBundle:Letter l
                  WHERE l.metadatas->:key = :value
                  ";

          $query = $em->createQuery($dql);
          $query->setParameter('key', $key);
          $query->setParameter('value', $value);


          return $query->getResult();
      } 

some infos :

php7.1, mariadb 10.2+, doctrine/dbal ^2.6, doctrine orm ^2.5

Thanks a lot.

Answer

Jannes Botis picture Jannes Botis · Mar 20, 2018

You can use ScientaNL/DoctrineJsonFunctions

Install it through composer by adding:

"scienta/doctrine-json-functions": "~4.0",

Register the json function that is needed in the doctrine configuration, in this case JSON_CONTAINS:

doctrine:
    orm:
        entity_managers:
            some_em: # usually also "default"
                dql:
                    string_functions:
                        JSON_CONTAINS: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonContains

In my case, I just added:

doctrine:
    orm:
         dql:
              string_functions:
                  JSON_CONTAINS: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonContains

Use it:

$queryBuilder = $this->getDoctrine()->getRepository('AppBundle:Letter')->createQueryBuilder('lt');
$queryBuilder
        ->where("JSON_CONTAINS(lt.metadatas, :mvalue, '$.key') = 1");

$queryBuilder->setParameter('mvalue', '"value"');
$query = $queryBuilder->getQuery();
return $query->getResult();

In dql, it should be something like:

$dql = "SELECT l FROM AppBundle:Letter l
              WHERE JSON_CONTAINS(lt.metadatas, :mvalue, '$.key') = 1
              ";

Note $.key is the json key to filter and mvalue should be included in its json encoded format, in this case with double quotes.

References:

MySql json-search-functions