插件窝 干货文章 高效处理 JSON 数据:scienta/doctrine-json-functions 库的使用指南

高效处理 JSON 数据:scienta/doctrine-json-functions 库的使用指南

json functions DoctrineJsonFunc scienta 390    来源:    2025-03-14

scienta/doctrine-json-functions 是一个用于 Doctrine ORM 的扩展库,它允许你在 DQL(Doctrine Query Language)中使用 JSON 函数来处理数据库中的 JSON 数据。这个库特别适用于那些需要在数据库中存储和查询 JSON 数据的应用程序。

安装

首先,你需要通过 Composer 安装这个库:

composer require scienta/doctrine-json-functions

配置

安装完成后,你需要在 Doctrine 的配置中注册这个库。通常,你可以在 config/packages/doctrine.yaml 文件中进行配置:

doctrine:
    orm:
        dql:
            string_functions:
                JSON_EXTRACT: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonExtract
                JSON_SEARCH: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonSearch
                JSON_CONTAINS: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonContains
                JSON_LENGTH: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonLength
                JSON_ARRAY: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonArray
                JSON_OBJECT: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonObject
                JSON_QUOTE: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonQuote
                JSON_UNQUOTE: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonUnquote
                JSON_MERGE: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonMerge
                JSON_SET: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonSet
                JSON_INSERT: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonInsert
                JSON_REPLACE: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonReplace
                JSON_REMOVE: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonRemove
                JSON_DEPTH: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonDepth
                JSON_TYPE: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonType
                JSON_VALID: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonValid
                JSON_KEYS: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonKeys
                JSON_ARRAYAGG: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonArrayAgg
                JSON_OBJECTAGG: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonObjectAgg

使用示例

假设你有一个 User 实体,其中有一个 profile 字段存储为 JSON 数据。你可以使用 JSON_EXTRACT 函数来查询特定的 JSON 字段。

use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\ORM\EntityManagerInterface;

class UserRepository extends \Doctrine\ORM\EntityRepository
{
    public function findUsersByCity(EntityManagerInterface $em, string $city)
    {
        $rsm = new ResultSetMapping();
        $rsm->addEntityResult('App\Entity\User', 'u');
        $rsm->addFieldResult('u', 'id', 'id');
        $rsm->addFieldResult('u', 'name', 'name');
        $rsm->addFieldResult('u', 'profile', 'profile');

        $query = $em->createNativeQuery('
            SELECT u.id, u.name, u.profile
            FROM user u
            WHERE JSON_EXTRACT(u.profile, "$.address.city") = :city
        ', $rsm);

        $query->setParameter('city', $city);

        return $query->getResult();
    }
}

支持的 JSON 函数

scienta/doctrine-json-functions 支持多种 JSON 函数,包括但不限于:

  • JSON_EXTRACT: 提取 JSON 文档中的特定部分。
  • JSON_SEARCH: 在 JSON 文档中搜索指定的值。
  • JSON_CONTAINS: 检查 JSON 文档是否包含指定的值。
  • JSON_LENGTH: 返回 JSON 文档的长度。
  • JSON_ARRAY: 创建一个 JSON 数组。
  • JSON_OBJECT: 创建一个 JSON 对象。
  • JSON_QUOTE: 将字符串转换为 JSON 字符串。
  • JSON_UNQUOTE: 将 JSON 字符串转换为普通字符串。
  • JSON_MERGE: 合并多个 JSON 文档。
  • JSON_SET: 在 JSON 文档中设置值。
  • JSON_INSERT: 在 JSON 文档中插入值。
  • JSON_REPLACE: 替换 JSON 文档中的值。
  • JSON_REMOVE: 从 JSON 文档中移除值。
  • JSON_DEPTH: 返回 JSON 文档的深度。
  • JSON_TYPE: 返回 JSON 值的类型。
  • JSON_VALID: 检查 JSON 文档是否有效。
  • JSON_KEYS: 返回 JSON 对象的所有键。
  • JSON_ARRAYAGG: 将多个 JSON 值聚合为一个 JSON 数组。
  • JSON_OBJECTAGG: 将多个键值对聚合为一个 JSON 对象。

注意事项

  • 这个库主要针对 MySQL 数据库。如果你使用的是其他数据库(如 PostgreSQL 或 SQLite),可能需要使用不同的 JSON 函数或扩展。
  • 在使用 JSON 函数时,确保你的数据库版本支持这些函数。

总结

scienta/doctrine-json-functions 是一个强大的工具,可以帮助你在 Doctrine ORM 中高效地处理 JSON 数据。通过使用这个库,你可以轻松地在 DQL 中执行复杂的 JSON 查询和操作,从而简化你的代码并提高开发效率。