bugloos/query-sorting-bundle 问题修复 & 功能扩展

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

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

bugloos/query-sorting-bundle

Composer 安装命令:

composer require bugloos/query-sorting-bundle

包简介

The query sorting bundle allows you to sort data from QueryBuilder and the Database. you can sort multiple columns at the same time and also you can sort relation fields with two-level deep and without any join in your query builder.

README 文档

README

Scrutinizer Code Quality GitHub Workflow Status Code Intelligence Status

What does it do? :)

The query sorting bundle allows you to sort data from QueryBuilder and the Database. you can sort multiple columns at the same time and also you can sort relation fields with two-level deep and without any join in your query builder.

Installation:

composer require bugloos/query-sorting-bundle

Compatibility

  • PHP v8.1 or above
  • Symfony v4.4 or above

Usage

Suppose our database has the following tables with the following relations

Service running preview

Now we want to show the Book entity by sorting

We want sort Book entity by price column, so we can send sort data by Querystring or make inline with array like this:

/*
 * Sort book by price ascending
*/
//Get api/book/index?order[price]=ASC
OR
$orders = [
    'price' => SortType::ASC,
];

/*
 * Sort book by price descending
*/
//Get api/book/index?order[price]=DESC
OR
$orders = [
    'price' => SortType::DESC,
];

You just need to add Sorting class in controller the call sort method:

use Bugloos\QuerySortingBundle\Service\QuerySorting;

The following code is in Book controller.

As you see, At first you should call for() method and pass QueryBuilder as parameter to this method

Then call parameters() method and pass orders request items

At the end you should call sort() method to run sorting

The return of sort method is Query Builder, so you can add anything else to Query Builder after sorting.
public function index(
    Request $request,
    BookRepository $bookRepository,
    QuerySorting $querySorting
): Response {
    $queryBuilder = $bookRepository->createQueryBuilder('b');
    
    $queryBuilder = $querySorting->for($queryBuilder)
        ->parameters($request->get('order'))
        ->sort()
    ;
    
    return $queryBuilder->getQuery()->getResult();
}

If you want to sort the ManyToOne relation field or one level deep relation, you should add mapper.

To add a mapper, you call addMapper() method to add single mapper or call mappers() method to send multiple mappers with array

First parameter of addMapper() method is parameter name and second parameter is relation name and its field name, which separate by " . " sign

$mappers = [
    'country' => 'country.name',
];

For example we want to sort Book entity by its Country name. Book has ManyToOne relation with Country entity

/*
 * Sort book by Country name ascending
*/
//Get api/book/index?order[country]=ASC
OR
$orders = [
    'country' => SortType::ASC,
];

/*
 * Sort book by Country name descending
*/
//Get api/book/index?order[country]=DESC
OR
$orders = [
    'country' => SortType::DESC,
];

The following code is in Book controller.

public function index(
    Request $request,
    BookRepository $bookRepository,
    QuerySorting $querySorting
): Response {
    $queryBuilder = $bookRepository->createQueryBuilder('b');
    
    $queryBuilder = $querySorting->for($queryBuilder)
        ->parameters($request->get('order'))
        ->addMapper('country', 'country.name')
        ->sort()
    ;
    
    return $queryBuilder->getQuery()->getResult();
}

NOTE: There is no need to add your relationship join in Query builder because if join is not added, I will add it automatically. ;)

$queryBuilder = $bookRepository->createQueryBuilder('b');

OR

$queryBuilder = $bookRepository->createQueryBuilder('b')
    ->addSelect('country')   
    ->leftJoin('b.country', 'country')      
;

If you want to sort the ManyToMany relation field or two level deep relation, you should again add mapper

$mapper = [
    'age' => 'bookUsers.user.age',
];

For example we want to sort Book entity by its Writer age. Book has ManyToMany relation with User entity

/*
 * Sort book by Writer age ascending
*/
//Get api/book/index?order[age]=ASC
OR
$orders = [
    'age' => SortType::ASC,
];

/*
 * Sort book by Writer age descending
*/
//Get api/book/index?order[age]=DESC
OR
$orders = [
    'age' => SortType::DESC,
];

The following code is in Book controller.

public function index(
    Request $request,
    BookRepository $bookRepository,
    QuerySorting $querySorting
): Response {
    $queryBuilder = $bookRepository->createQueryBuilder('b');
    
    $queryBuilder = $querySorting->for($queryBuilder)
        ->parameters($request->get('order'))
        ->addMapper('age', 'bookUsers.user.age')
        ->sort()
    ;
    
    return $queryBuilder->getQuery()->getResult();
}

NOTE: You should know that you can sort data with multiple columns too, you just need to send multiple order data with a Query string like this:

/*
 * Sort book by title ascending and price descending
*/
//Get api/book/index?order[title]=ASC&order[price]=DESC
OR
$orders = [
    'title' => SortType::ASC,
    'price' => SortType::DESC,
];

Suggestion

You can change two parameters with the config file, just make a yaml file in config/packages/ directory then you can change default cache time for queries and default relation separator as follows:

query_sorting:
  default_cache_time: 3600
  separator: '.'

NOTE: You can set the cache time for each query separately and if you don't set any cache time, it uses default cache time in your config file

$queryBuilder = $querySorting->for($queryBuilder)
    ->parameters($request->get('order'))
    ->cacheTime(120)
    ->sort()
;

Contributing v beer

If you find an issue, or have a better way to do something, feel free to open an issue or a pull request.

bugloos/query-sorting-bundle 适用场景与选型建议

bugloos/query-sorting-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.22k 次下载、GitHub Stars 达 13, 最近一次更新时间为 2022 年 05 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 5.22k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 13
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-05-16