PHPackages                             koine/query-builder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. koine/query-builder

ActiveLibrary[Database &amp; ORM](/categories/database)

koine/query-builder
===================

Query Builder - Making SQL composing easier

0.9.7(11y ago)719.9k3MITPHP

Since Jul 2Pushed 9y ago2 watchersCompare

[ Source](https://github.com/koinephp/QueryBuilder)[ Packagist](https://packagist.org/packages/koine/query-builder)[ RSS](/packages/koine-query-builder/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (4)Used By (0)

Koine Query Builder
===================

[](#koine-query-builder)

Query Builder for easing the SQL composing

Code information:

[![Build Status](https://camo.githubusercontent.com/ade6a8817a5f54b5a7f2e571056baa6a122f617ecc768bbb7e1ed92db80adbd8/68747470733a2f2f7472617669732d63692e6f72672f6b6f696e657068702f51756572794275696c6465722e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/koinephp/QueryBuilder)[![Coverage Status](https://camo.githubusercontent.com/d4c336f3a0d83fb1fe7bfe3ddc5587e97523af73812924f88e56f8edfb616544/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6b6f696e657068702f51756572794275696c6465722f62616467652e706e67)](https://coveralls.io/r/koinephp/QueryBuilder)[![Code Climate](https://camo.githubusercontent.com/df40a8a0805da4c075bc160704497f7df5df36dc48b963343c7552d469e0c403/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6b6f696e657068702f51756572794275696c6465722e706e67)](https://codeclimate.com/github/koinephp/QueryBuilder)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/3896eb2a463e41bffca59e60ec00b54cd3e0938e36fcc720d0ecd9a5e975dc81/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6b6f696e657068702f51756572794275696c6465722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/koinephp/QueryBuilder/?branch=master)

Package information:

[![Latest Stable Version](https://camo.githubusercontent.com/0dd3a3a57c1d0c9d0823e11086431e84f9e2bd7331c125afdd8c9e93558cff34/68747470733a2f2f706f7365722e707567782e6f72672f6b6f696e652f71756572792d6275696c6465722f762f737461626c652e737667)](https://packagist.org/packages/koine/query-builder)[![Total Downloads](https://camo.githubusercontent.com/12dd346a9fb12fe593ece810494bbee6ab703c2fdebb3eb7dc197fe65e22f9ed/68747470733a2f2f706f7365722e707567782e6f72672f6b6f696e652f71756572792d6275696c6465722f646f776e6c6f6164732e737667)](https://packagist.org/packages/koine/query-builder)[![Latest Unstable Version](https://camo.githubusercontent.com/4a0d8222044e3d216ead9e3ab7faee806322726392a2f29c21d713eb52b1c996/68747470733a2f2f706f7365722e707567782e6f72672f6b6f696e652f71756572792d6275696c6465722f762f756e737461626c652e737667)](https://packagist.org/packages/koine/query-builder)[![License](https://camo.githubusercontent.com/fdaf797c91393d3011c8323aa6937aa2d08df72dae3b742e876d1bbe16289f36/68747470733a2f2f706f7365722e707567782e6f72672f6b6f696e652f71756572792d6275696c6465722f6c6963656e73652e737667)](https://packagist.org/packages/koine/query-builder)[![Dependency Status](https://camo.githubusercontent.com/cb10fc9845f013d3586d9730bc96e6047ba1a660c9dce7bd1bba84887889b4f6/68747470733a2f2f67656d6e617369756d2e636f6d2f6b6f696e657068702f51756572794275696c6465722e706e67)](https://gemnasium.com/koinephp/QueryBuilder)

Installing
----------

[](#installing)

### Installing via Composer

[](#installing-via-composer)

Append the lib to your requirements key in your composer.json.

```
{
    // composer.json
    // [..]
    require: {
        // append this line to your requirements
        "koine/query-builder": "dev-master"
    }
}
```

### Alternative install

[](#alternative-install)

- Learn [composer](https://getcomposer.org). You should not be looking for an alternative install. It is worth the time. Trust me ;-)
- Follow [this set of instructions](#installing-via-composer)

Usage
-----

[](#usage)

### SELECT

[](#select)

This is an example of select query.

- Applies [limit](#limit)
- Applies [where](#where)
- Applies [orderBy](#order-by)
- Applies [groupBy](#group-by)
- Applies [placeholders](#using-placeholders)

```
$fields = array('u.name AS name', 'r.name AS role');

// Selecting via factory
$select = Koine\QueryBuilder::factorySelect($fields);

// Selecting via the select method
$select = Koine\QueryBuilder::factorySelect()
    ->select($fields);

// or alternatively
$select = new Koine\QueryBuilder\Statements\Select();
$select->select($fields);

// From
$select->from('users u');

// Adding joins
$select->innerJoin('roles r', 'u.id = r.user_id');

$select->toSql();

// SELECT u.name AS name, r.name AS role
// FROM users u INNER JOIN roles r ON u.idi = r.user_id
```

### INSERT

[](#insert)

- Applies [placeholders](#using-placeholders)

```
// Using the factory
$insert = Koine\QueryBuilder::insert();

// Or alternatively
$insert = new Koine\QueryBuilder\Statements\Insert();

$insert->into('users')->values(array(
    'name'  => 'Jon Doe',
    'email' => 'jon@doe.com'
));

$insert->toSql();

// INSERT INTO users (name, email) VALUES ('Jon Doe', 'jon@doe.com');
```

### UPDATE

[](#update)

- Applies [limit](#limit)
- Applies [where](#where)
- Applies [orderBy](#order-by)
- Applies [groupBy](#group-by)
- Applies [placeholders](#using-placeholders)

```
$update = Koine\QueryBuilder::update('users');

// or
$update = new Koine\QueryBuilder\Statements\Update;
$update->table('users');

// setting values and conditions

$update->set(array(
        'enabled' => 1
    ))->where('email', ':email');

$update->toSql(array(
    'email' => 'admin@email.com'
));

// UPDATE users SET enabled = 1 WHERE email = 'admin@email.com'
```

### DELETE

[](#delete)

TODO: Implement

### WHERE

[](#where)

Every time a `where()` method is called, the condition is added to the query.

```
// method signature
$query->where($field, $value, $operator);

// or
$query->where($condition);

// or
$query->where(array(
    array($field, $value, $operator),
    array($condition),
));

// Below some valid examples:

$query->where('email', 'admin@abc.com');
// WHERE email = 'admin@abc.com'

$query->where('email', 'admin@abc.com', '');
// WHERE email  "admin@abc.com"

$query->where('email', '%@google.com', 'LIKE');
// WHERE email LIKE "%@google.com"

$query->where('age', 20);
// WHERE age = 20

$query->where('code', 001);
// WHERE code = 001

$query->where('code', array('value' => '001'));
// WHERE code = '001'

$query->where('(code = 1 OR code = 2)'));
// WHERE (code = 1 OR code = 2)

// multiple conditioins, one method call
$query->where(array(
    array('email', 'admin@abc.com', ''),
    array('email', '%@google.com', 'LIKE'),
    array('age', 20),
    array('(code = 1 OR code = 2)'),
    array('hash', array('value' => 'SOMEFUNCTION()')),
));

// WHERE condition 1 AND condition 2..
```

### ORDER BY

[](#order-by)

```
$query->orderBy('name DESC');
// or
$query->orderBy(array('name DESC', 'age ASC'));
```

### GROUP BY

[](#group-by)

```
$query->groupBy('a, b, c');
// or
$query->groupBy(array('a', 'b', 'b'));
```

### LIMIT

[](#limit)

```
$query->limit(2);
$query->limit(2, 1);
```

### Using placeholders

[](#using-placeholders)

Placeholders are a good way for building your queries when you don't know what values are going to be used (because they depend on the result of a query yet to be executed, for instance).

```
$insert->into('users')->values(array(
    'name'  => ':name',
    'email' => ':email'
));

$insert->toSql(array(
    'name'  => 'Jon Doe',
    'email' => 'jon@doe.com'
));

// INSERT INTO users (name, email) VALUES ('Jon Doe', 'jon@doe.com');
```

Issues/Features proposals
-------------------------

[](#issuesfeatures-proposals)

[Here](https://github.com/koinephp/query-builder/issues) is the issue tracker.

Contributing
------------

[](#contributing)

Only TDD code will be accepted. Please follow the [PSR-2 code standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md).

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

### How to run the tests:

[](#how-to-run-the-tests)

```
phpunit --configuration tests/phpunit.xml
```

### To check the code standard run:

[](#to-check-the-code-standard-run)

```
phpcs --standard=PSR2 lib
phpcs --standard=PSR2 tests

# alternatively

./bin/travis/run_phpcs.sh
```

License
-------

[](#license)

[MIT](MIT-LICENSE)

Authors
-------

[](#authors)

- [Marcelo Jacobus](https://github.com/mjacobus)

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~25 days

Total

4

Last Release

4263d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/226834?v=4)[Marcelo Jacobus](/maintainers/mjacobus)[@mjacobus](https://github.com/mjacobus)

---

Top Contributors

[![mjacobus](https://avatars.githubusercontent.com/u/226834?v=4)](https://github.com/mjacobus "mjacobus (114 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/koine-query-builder/health.svg)

```
[![Health](https://phpackages.com/badges/koine-query-builder/health.svg)](https://phpackages.com/packages/koine-query-builder)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90440.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)[wildside/userstamps

Laravel Userstamps provides an Eloquent trait which automatically maintains `created\_by` and `updated\_by` columns on your model, populated by the currently authenticated user in your application.

7511.7M13](/packages/wildside-userstamps)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
