warrantgroup/doctrine-query-builder

一个优雅的轻量级库,可以动态地从结构化数据构建 doctrine 查询。非常适合 API 解析数据应该如何表示,无需任何额外的映射。

0.1.1 2017-05-14 14:49 UTC

This package is not auto-updated.

Last update: 2024-09-20 02:07:09 UTC


README

一个优雅的轻量级库,可以动态地从结构化数据构建 doctrine 查询。非常适合 API 解析数据应该如何表示,无需任何额外的映射。

支持无限嵌套的 AND/OR 组,大多数常见的 SQL 操作符,连接,排序,distinct 等

要求

  • PHP >= 5.6
  • Symfony HTTP Foundation > 3.2
  • Doctrine ORM > 2.5

安装

使用 Composer

{
    "require": {
        "warrantgroup/doctrine-query-builder": "dev-master"
    }
}

用法

use \Warrant\Doctrine\QueryBuilder\QueryBuilder;

$repo = $this->getDoctrine()->getManager()->getRepository('AcmeMainBundle:Person');
$qb = new QueryBuilder();

$results = $qb->build($repo, $data)->getQuery()->getResult();

return new JsonResponse($results);

示例数据

{
    "alias": "p",
    "select": ["p.id"],
    "where": {
        "$or": {
            "p.city": {
                "$same": "c.city"
            },
            "p.zipCode": {
                "$same": "c.zipCode"
            },
            "p.street": {
                "$same": "c.street"
            },
        },
        "c.city": {
            "$in": [
                "New York",
                "London"
            ]
        },
        "c.employees": { "$equals": 1 },
        "l.code": 49,
        "p.country": "$not_null",
        "p.phone": "$is_null",
        "c.assets": { "$gte": 1000 },
        "c.turnover": { "$lt": 10000 },
        "t.code": {
            "$in": [1, 2, 3]
        },
        "r.title": {
            "$not_in": ":titles"
        }
    },
    "distinct": true,
    "params": {
        "titles": ["CFO", "CMO"]
    },
    "orderBy": {
        "p.name": "asc"
    },
    "join": {
        "p.roles": "r",
        "r.company": "c",
        "c.trades": {
            "alias": "t",
            "type": "left"
        }
    }
}