onemustcode/query 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

onemustcode/query

Composer 安装命令:

composer require onemustcode/query

包简介

It simply transforms the given filters, sortings and paging to a Doctrine or Eloquent query. Handy for interal usage or it can be used for API's.

README 文档

README

Build Status Total Downloads Latest Stable Version License

Query

It simply transforms the given filters, sortings and paging to a Doctrine or Eloquent query. Handy for interal usage or it can be used for API's.

Installation

Require the Query library trough composer.

composer require onemustcode/query

Query

The Query holds the paging, sortings, filters and includes.

Create new Query instance;

$query = new Query();

Paging

Change the results per page;

$query->getPaging()->setPerPage(50);

Change the page;

$query->getPaging()->setPage(5);

Create an Query instance directly from given Paging object;

$query = Query::createFromPaging(new Paging(3, 40));

Filtering

The following filters can be used;

Type Class
Equals new Equals('field', 'value')
Not Equals new NotEquals('field', 'value')
Greather than new GreatherThan('field', 20)
Greather than or Equals new GreatherThanOrEquals('field', 25)
Less than new LessThan('field', 50)
Less than or Equals new LessThanOrEquals('field', 55)
Is null new IsNull('field')
Is not null new IsNotNull('field')
In new In('field', '1,2,3,4,5')
Not in new NotIn('field', '1,2,3,4,5')
Like new Like('field', '%ohn%')

Add new filter to existing Query instance;

$query->addFilter(new Equals('last_name', 'john'));

Create an Query instance directly from one or more filters;

$query = Query::createFromFilters([
    new Equals('name', 'john),
    new GreatherThan('age', 23),
]);

Sorting

Add new sorting to existing Query instance;

$query->addSorting(new Ascending('last_name'));

Create an Query instance directly from one or more sortings;

$query = Query::createFromSortings([
    new Ascending('name'),
    new Descending('score'),
]);

Includes

Add new include to existing Query instance;

$query->addInclude('posts.comments);

Create an Query instance directly from one or more includes;

$query = Query::createFromIncludes([
    'relation', 'some_other.relation'
]);

Builder

Warning: The Builder class is deprecated, use the transformer instead.

The builder turns an given array to an Query instance;

$data = [
    'per_page' => 20,
    'page' => 2,
    'filters' => [
        'age' => ['eq' => 15],
        'last_name' => ['like' => 'doe%'],
    ],
    'sortings' => [
        'first_name' => 'asc',
        'score' => 'desc',
    ],
];

$builder = new Builder($data);
$query = $builder->build();

Transformer

The builder transforms an given array to an Query instance;

$data = [
    'per_page' => 20,
    'page' => 2,
    'filters' => [
        'age' => ['eq' => 15],
        'last_name' => ['like' => 'doe%'],
    ],
    'sortings' => [
        'first_name' => 'asc',
        'score' => 'desc',
    ],
];

$transformer = new Transformer();
$query = $transformer->transform($data);

It is possible to add your own custom filters. In order to do this you should pass them as an associative array in the constructor (note that the filter should implement the OneMustCode\Query\Filters\FilterInterface]). The key should be the filters's operator and the value the filter's FQN;

$transformer = new Transformer([CustomFilter::OPERATOR => CustomFilter::class]);
$query = $transformer->transform($data);

Writer

The writer can export the given Query instance to json, query parameters and an array.

Json

$writer = new Writer($query);
$parameters = $writer->toJson();

Array

$writer = new Writer($query);
$parameters = $writer->toArray();

Query parameters

$writer = new Writer($query);
$parameters = $writer->toQueryParameters();

Doctrine Query Builder

The following example code shows you how to use the Doctrine Query Builder. Just pass the Query instance, Doctrine Query Builder, the accepted filters or sortings and it will automatically generate the query for you.

Example;

// Get the Doctrine Query Builder
$queryBuilder = $this->createQueryBuilder('e');

// Create new Query instance from given filters en sortings
$query = new Query(null, [
    new GreatherThan('age', 25),
    new LessThan('age', 55),
], [
    new Ascending('last_name')),
]);

// Only accept certain sortings that can be added to the doctrine query builder
$acceptedSortings = [
    // Given field => Doctrine property
    'age' => 'e.age',
];

// Only accept certain filters that can be added to the doctrine query builder
$acceptedFilters = [
    // Given field => Doctrine property
    'first_name' => 'e.firstName',
    'last_name' => 'e.lastName',
    'age' => 'e.age',
];

// Build the query
$queryBuilder = (new QueryBuilder())->build($query, $queryBuilder, $acceptedFilters, $acceptedSortings);

// Retrieve the results via Doctrine
$results = $queryBuilder->getQuery()->getResults();

It is possible to add your own custom filter handlers. In order to do this you should pass in the handler in the QueryBuilder's constructor (note that the filter handler should implement the OneMustCode\Query\Builders\Doctrine\Filters\FilterHandlerInterface);

$queryBuilder = (new QueryBuilder([new CustomFilterHandler()]))->build($query, $queryBuilder, $acceptedFilters, $acceptedSortings);

License

MIT

onemustcode/query 适用场景与选型建议

onemustcode/query 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27.37k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2017 年 02 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「paging」 「api」 「doctrine」 「query」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 onemustcode/query 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 onemustcode/query 我们能提供哪些服务?
定制开发 / 二次开发

基于 onemustcode/query 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 27.37k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 19
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-02-04