paknahad/querifier

一个库,用于从客户端获取请求的查询并生成针对 Doctrine、Cake-php ORM 或 Laravel Eloquent 的查询

v1.0.0 2021-10-14 12:06 UTC

This package is auto-updated.

Last update: 2024-08-29 05:12:30 UTC


README

Latest Stable Version Build Status License: MIT

Querifier

安装

composer require paknahad/querifier

用法

Symfony & Doctrine

<?php
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
use Paknahad\Querifier\Filter;
...
    $psrFactory = new DiactorosFactory();
    $psrRequest = $psrFactory->createRequest($request);

    $filter = new Filter($psrRequest);
    $filter->applyFilter($repository->createQueryBuilder('alias'));

示例

表达式

http://example.com/books?q=title:php^author.name:hamid

条件

http://example.com/books?filter[title]=php&filter[author.name]=hamid

SQL

SELECT * FROM books AS b INNER JOIN authors AS a ...
WHERE
    b.title = 'php'
    AND
    a.name = 'hamid'

高级查询

表达式

http://example.com/books?q=title:php^(author.name:%hamid%|(publish_date>2017-1-1^publish_date<2017-6-1))

条件

books?filter[title]=php&filter[_c1][author.name][_like]=%hamid%&filter[_c2][publish_date][_gt]=2017-1-1&filter[_c3][publish_date][_lt]=2017-6-1]&filter[_c4][_cmb_and]=_c2,_c3&filter[_cmb_or]=_c4,_c1

SQL

SELECT * FROM books AS b INNER JOIN authors  AS a …
WHERE
    b.title = 'php' 
    AND
    (
        (
            b.publish_date > '2017-1-1'
            AND
            B.publish_date < '2017-6-1'
        )
        OR
        a.name LIKE '%hamid%'
     )

表达式解析器

简单查询

简单查询由以下结构生成

example.com?q=FIELD_NAME:VALUE

高级查询

逻辑运算符

  • ^ -> AND
  • | -> OR

比较运算符

  • : -> EqualLIKE 如果值中存在 %
  • :null -> IS NULL
  • <> -> Not EqualNOT LIKE 如果值中存在 %
  • <>null -> IS NOT NULL
  • > -> Greather than
  • < -> Less than

条件解析器

简单查询

简单查询由以下结构生成

url?filter[FIELD_NAME]=VALUE

高级查询

1- 定义条件。

url?filter[CONDITION NAME][FIELD NAME][OPERATOR]=VALUE

2- 将这些条件组合在一起。

url?filter[CONDITION NAME][COMBINE]=CONDITIONS SEPARATED BY “,”
  • 条件名称(可选) : 用于组合的标识符,必须以“_”开头,后跟字母数字字符
  • 运算符名称(可选,默认: _eq) : 运算符的名称,例如 _eq, _not_eq, _in, _gt, _lt, _like。
  • 组合 : 用于组合两个或多个条件:_cmb_or, _cmb_and

排序

  • 按名称字段升序排序: http://example.com/books?sort=name
  • 按名称字段降序排序: http://example.com/books?sort=-name
  • 多个字段: http://example.com/books?sort=city,-name
  • 关联字段: http://example.com/books?sort=author.name