Skip to content
Small query builder for PHP
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples
src
tests
.gitignore
.php_cs.dist
LICENSE
README.md
composer.json
phpstan.neon.dist
phpunit.xml.dist

README.md

Query Builder for PHP

A fork of Muhammad Usman's Pixie, a lightweight query builder for PHP. This library only supports a subset of Pixie's fetures:

  • Nested criterias (WHERE, HAVING and ON for joins)
  • Raw queries

Query Builder is not able to open database connections or execute the built queries.

Install

You can install Query Builder via Composer:

composer require kekos/query-builder

API

Start by configure Query Builder with an adapter:

QueryBuilder::setAdapter(new MySqlAdapter());

The adapter sets the correct sanitizer character. At this moment MySqlAdapter is provided with the library.

All builders have a toSql() method, which returns the SQL query and all parameters to be bound to the query, to be used with prepared statements.

Select

$result = QueryBuilder::select(['user', 'u'])
  ->columns(['u.id', 'uname' => 'username'])
  ->join(['user_permission', 'p'], QB::raw('p.user_id = u.id'))
  ->limit(5, 0)
  ->groupby(['u.id'])
  ->orderby(['username ASC', 'firstname ASC'])
  ->where('firstname', '=', 'Christoffer')
  ->whereNot('u.id', 'IN', array(2))
  ->toSql();

$result will be an object of type QueryBuilder\QueryBuilders\Raw.

Nested where or having

$result = QueryBuilder::select('user')
  ->orderby('id')
  ->where(function($qb) {
    $qb->where('name', 'LIKE', '%chris%')
      ->whereOr('username', 'LIKE', '%chris%');
  })
  ->where('active', '=', 1)
  ->toSql();

Insert

$result = QueryBuilder::insert('user')
  ->values([
      'username' => 'Kekos',
      'firstname' => 'Christoffer'
    ])
  ->toSql();

Update

$result = QueryBuilder::update('user')
  ->set([
      'username' => 'new_username',
      'firstname' => 'New firstname'
    ])
  ->where('id', '=', 2)
  ->toSql();

Delete

$result = QueryBuilder::delete('user')
  ->where('id', '=', 2)
  ->toSql();

Bugs and improvements

Report bugs in GitHub issues or feel free to make a pull request :-)

License

MIT

You can’t perform that action at this time.