承接 sultann/wp-query-builder 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

sultann/wp-query-builder

Composer 安装命令:

composer require sultann/wp-query-builder

包简介

WP Query Builder is a lightweight and efficient SQL Query Builder based on wpdb for WordPress. It supports complicated query generation.

README 文档

README

WP Query Builder

WP Query Builder is a lightweight and efficient SQL Query Builder based on wpdb for WordPress. It supports complicated query generation.

Build Status Packagist Packagist GitHub release

Installation

WP Query Builder follows PSR-4 autoloading and can be installed using composer:

$ composer require sultann/wp-query-builder

Documentation 💡

Initialize

Initialize query builder. init method takes a string argument using that later you can do action/filter based on your requirement. without argument.

$query = \PluginEver\QueryBuilder\Query::init();

with argument

$query = \PluginEver\QueryBuilder\Query::init('query_users');

Select

This will build the query, execute and returns all users from users table with applying table prefix automatically. by default it select all(*) but you can define what to select from the query; If you are selecting all then you can omit the select statement.

$results = $query->select('*')
                 ->from('users')
                 ->get();

Select specific column

$results = $query->select('ID')
                 ->from('users')
                 ->get();

Select multiple column

$results = $query->select('user_login, user_email')
                 ->from('users')
                 ->get();

Where conditions

For the next few examples, lets assume a larger dataset so that the queries make sense.

$results = $query->select('*')
                 ->from('users')
                 ->where('user_email', 'like', '%gmail.com')
                 ->get();

Notice how omitting the operator in the first condition ->where('user_url', '') makes this default to =. By default all where conditions are defined with the and operator.

Different where operators:

$results = $query->select('*')
                 ->from('users')
                 ->where('user_email', 'like', '%gmail.com')
                 ->orWhere('user_email', 'like', '%yahoo.com')
                 ->get();

There are few more builtin Where conditions available

  • andWhere()
  • whereIn()
  • whereNotIn()
  • whereNull()
  • whereNotNull()
  • orWhereNull()
  • orWhereNotNull()
  • whereBetween()
  • whereNotBetween()
  • whereDateBetween()
  • whereRaw()

Where scopes

Allow you to group conditions:

$results = $query->select('*')
                 ->from('posts')
                ->where('post_status', 'publish')
                ->where(function($q) 
                {
                    $q->where('menu_order', '>', 21);
                    $q->where('menu_order', '<', 99);
                })
                ->orWhere('post_type', 'page')
                ->get();

Where Between

$results = $query->select('*')
                 ->from('posts')
                 ->whereBetween('menu_order', 1, 20)
                 ->get();

Where Not Between

$results = $query->select('*')
                 ->from('posts')
                 ->whereNotBetween('menu_order', 20, 30)
                 ->get();

Where Date Between

$results = $query->select('*')
                 ->from('posts')
                 ->whereDateBetween('post_date',  '2010-04-22 10:16:21', '2020-05-04')
                 ->get();

Joins

By default, all joins are Left Join. Available join types 'LEFT', 'RIGHT', 'INNER', 'CROSS', 'LEFT OUTER', 'RIGHT OUTER' Joining tables:

$results = $query->select( '*' )
                ->from( 'posts p' )
                ->join( 'users u', 'u.ID', '=','p.post_author' )
                ->get();

Joins scopes

Allow you to group conditions:

$results = $query->select( '*' )
                ->from( 'posts p' )
                ->join( 'users u', 'u.ID', '=','p.post_author' )
                ->join('usermeta um', function($q) {
                      $q->where('um.meta_key', 'first_name');
                      $q->where('um.met_value', 'like', '%sultan%');
                  })
                ->get();

There are few more builtin join conditions available

  • leftJoin()
  • rightJoin()
  • innerJoin()
  • outerJoin()

Grouping

Grouping data:

$results = $query->select('*')
                 ->from('posts')
                ->group_by('post_status')
                ->get();

Having

Group by with having data:

$results = $query->select('*')
                 ->from('posts')
                ->group_by('post_status')
                ->having('count(ID)>1')
                ->get();

Ordering

Ordering data:

$results = $query->select('*')
                 ->from('posts')
                ->order_by('post_title', 'DESC')
                ->get();

Limiting data

Limit and offset:

$results = $query->select('*')
                 ->from('posts')
                ->limit(20, 10)
                ->get();

Only limit

$results = $query->select('*')
                 ->from('posts')
                ->limit(20)
                ->get();

Offset as separate

$results = $query->select('*')
                 ->from('posts')
                ->limit(20)
                ->offset(10)
                ->get();

Pagination

shortcut of limit and offset

$results = $query->select('*')
                 ->from('posts')
                ->page(1, 20)//page number & page size
                ->get();

Find

find item with column value

$results = $query->select('*')
                 ->from('posts')
                 ->find(1, 'ID');

First

Get first item from the posts table

$results = $query->select('*')
                 ->from('posts')
                 ->first();

Last

Get last item from the posts table

$results = $query->select('*')
                 ->from('posts')
                 ->last();

Counting

count total rows

$results = $query->select('*')
                 ->from('posts')
                 ->count();

toSql

Out the query instead of executing

$results = $query->from('posts as p')
                ->join('users as u',  'p.post_author', 'u.ID')
                ->join('usermeta um', function($q) {
                      $q->where('um.meta_key', 'first_name');
                      $q->where('um.met_value', 'like', '%sultan%');
                  })
                ->toSql();

Update

Update a row

$results = $query->table('posts')
                 ->where('ID', 20)
                 ->update(['post_title' => 'updated']);

Delete

Delete a row

$results = $query->from('posts')
                ->where('ID', 20)
                ->delete();

Search

Search a value from columns

$results = $query->from('posts')
                 ->search('Hello Word', array('post_title', 'post_content')) // it will search Hello & World both
                 ->delete();

License

The MIT License (MIT). Please see License File for more information.

sultann/wp-query-builder 适用场景与选型建议

sultann/wp-query-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.73k 次下载、GitHub Stars 达 70, 最近一次更新时间为 2020 年 05 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 4.73k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 71
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 70
  • Watchers: 0
  • Forks: 14
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-05-03