timgws/query-builder-parser
Composer 安装命令:
composer require timgws/query-builder-parser
包简介
Build complex Eloquent & QueryBuilder queries automatically when using jQuery-QueryBuilder
README 文档
README
| Status Label | Status Value |
|---|---|
| Build | |
| Test Coverage |
QueryBuilderParser is designed mainly to be used inside Laravel projects, however it can be used outside Laravel projects by using Illuminate/Database.
A simple to use query builder for the jQuery QueryBuilder plugin.
Using QueryBuilderParser
Building a new query from QueryBuilder rules.
use timgws\QueryBuilderParser; $table = DB::table('table_of_data_to_integrate'); $qbp = new QueryBuilderParser( // provide here a list of allowable rows from the query builder. // NOTE: if a row is listed here, you will be able to create limits on that row from QBP. array( 'name', 'email' ) ); $query = $qbp->parse($input['querybuilder'], $table); $rows = $query->get(); return Response::JSON($rows);
This query when posted will create the following SQL query:
SELECT * FROM table_of_data_to_integrate WHERE `name` LIKE '%tim%' AND `email` LIKE '%@gmail.com'
Getting results from MongoDB
use timgws\QueryBuilderParser; $table = DB::collection('data'); $qbp = new QueryBuilderParser( // provide here a list of allowable rows from the query builder. // NOTE: if a row is listed here, you will be able to create limits on that row from QBP. array( 'name', 'email' ) ); $query = $qbp->parse($input['querybuilder'], $table); $rows = $query->get(); return Response::JSON($rows);
This query when posted will create the following MongoDB query:
{
"$and": [
{
"name": {
"$regex": "tim"
}
},
{
"email": {
"$regex": "@gmail\\.com$"
}
}
]
}
Note that to use this you will need to install and configure jenssegers/mongodb.
Integration examples
Integrating with jQuery Datatables
Mixed with Datatables, jQuery QueryBuilder makes for some true awesome, allowing limitless options for filtering data, and seeing the results on the fly.
use timgws\QueryBuilderParser; class AdminUserController { function displayUserDatatable() { /* builder is POST'd by the datatable */ $queryBuilderJSON = Input::get('rules'); $show_columns = array('id', 'username', 'email_address'); $query = new QueryBuilderParser($show_columns); /** Illuminate/Database/Query/Builder $queryBuilder **/ $queryBuilder = $query->parse(DB::table('users')); return Datatable::query($queryBuilder) ->showColumns($show_columns) ->orderColumns($show_columns) ->searchColumns($show_columns) ->make() } }
On the client side, a little bit of magic is required to make everything work.
// the default rules, what will be used on page loads... var datatablesRequest = {}; var _rules = defaultRules = {"condition":"AND","rules":[ {"id":"active","field":"active","type":"integer","input":"radio","operator":"equal","value":"1"} ]}; // a button/link that is used to update the rules. function updateFilters() { _rules = $('#querybuilder').queryBuilder('getRules'); reloadDatatables(); } function filterChange() { var _json = JSON.stringify( _rules ); datatablesRequest = { rules: _json }; } filterChange(); function reloadDatatables() { /* Datatables first... */ filterChange(); $('.dataTable').each(function() { dt = $(this).dataTable(); dt.fnDraw(); }) } jQuery(document).ready(function(){ // dynamic table oTable = jQuery('.datatable').dataTable({ "fnServerParams": function(aoData) { // add the extra parameters from the jQuery QueryBuilder to the Datatable endpoint... $.each(datatablesRequest , function(k,v){ aoData.push({"name": k, "value": v}); }) } }) });
Using JoinSupportingQueryBuilderParser
JoinSupportingQueryBuilderParser is a version of QueryBuilderParser that supports building even more complex queries.
$joinFields = array( 'join1' => array( 'from_table' => 'master', 'from_col' => 'm_col', 'to_table' => 'subtable', 'to_col' => 's_col', 'to_value_column' => 's_value', ), 'join2' => array( 'from_table' => 'master2', 'from_col' => 'm2_col', 'to_table' => 'subtable2', 'to_col' => 's2_col', 'to_value_column' => 's2_value', 'not_exists' => true, ) ); $table = DB::table('table_of_data_to_integrate'); $jsqbp = new JoinSupportingQueryBuilderParser($fields, $this->getJoinFields()); $test = $jsqbp->parse($json, $builder);
Which will build an SQL query similar to:
select * where exists (select 1 from `subtable` where subtable.s_col = master.m_col and `s_value` < ?)
For simple queries, QueryBuilderParser should be enough.
Exporting CSV files
Just as a footnote, there are right ways to export CSV files, and there are wrong ways.
For the right way, check out the question on StackOverflow, How can I output a UTF-8 CSV in PHP that Excel will read properly?
Reporting Issues
I use this code in a number of my projects, so if you do find an issue, please feel free to report it with GitHub's bug tracker for this project.
Alternatively, fork the project and make a pull request :)
timgws/query-builder-parser 适用场景与选型建议
timgws/query-builder-parser 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 404.17k 次下载、GitHub Stars 达 161, 最近一次更新时间为 2014 年 07 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「mongodb」 「sql」 「jquery」 「parser」 「ajax」 「query builder」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 timgws/query-builder-parser 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 timgws/query-builder-parser 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 timgws/query-builder-parser 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Generates Symfony2 documents, forms and CRUD
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
Query filtering in your frontend
A package to allow laravel/passport use with mongodb/laravel-mongodb
Database/ORM-agnostic query construction helper
This is a laravel 4 package for the server and client side of datatables at http://datatables.net/
统计信息
- 总下载量: 404.17k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 161
- 点击次数: 46
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-07-31
