codemagpie / simple-query-builder
A simple and general query builder
1.2.0
2022-06-09 03:53 UTC
Requires
- php: >=7.4
- codemagpie/utils: ^1.0
- hyperf/database: ^2.2
- ruflin/elastica: ^7.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.1
- phpunit/phpunit: ^9.5
README
Introduction
A simple and general query builder,You can use this builder is transformed into other query-builder, such as sql, mongodb, elasticsearch...
Installation
composer require codemagpie/simple-query-builder
Usage
Only need to extend the class \CodeMagpie\SimpleQueryBuilder\AbstractSimpleQuery :
use CodeMagpie\SimpleQueryBuilder\AbstractSimpleQuery; class UserQuery extends AbstractSimpleQuery { /** * @var string[] allow the query field, ['*'] is allow all */ protected array $fields = ['id', 'name', 'age']; }
To use:
$query = UserQuery::build() ->whereIn('id', [1, 2]) ->whereEqual('name', 'test') ->addNestedOrWhere(function ($query) { $query->whereGreat('age', 10)->orWhereLess('age', 8); }) ->orderByDesc('id') ->forPage(1, 10); // This is similar to "where id in (1, 2) and name = 'test' or (age > 10 or age < 8) order by id desc limit 10"
Supports
- support convert ruflin/elastica query, use:
$query = UserQuery::build()->whereEqual('name', 'test')->bindElasticaQueryBuilder(new \Elastica\Query());
- support convert hyperf/database query, use:
$builder = Model::query(); // hyperf Model, such as UserModel $query = UserQuery::build()->whereEqual('name', 'test')->bindHyperfQueryBuilder($builder)