PHPackages                             php-objects/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. php-objects/query-builder

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

php-objects/query-builder
=========================

Query Builder - Making SQL composing easier

0.9.4(11y ago)107769[1 issues](https://github.com/mjacobus/php-query-builder/issues)MITPHP

Since Jun 23Pushed 11y ago2 watchersCompare

[ Source](https://github.com/mjacobus/php-query-builder)[ Packagist](https://packagist.org/packages/php-objects/query-builder)[ RSS](/packages/php-objects-query-builder/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (7)Versions (5)Used By (0)

PO Query Builder
================

[](#po-query-builder)

Query Builder for easing the SQL composing

Important notice: Deprecated in favor of [Koine/QueryBuilder](https://github.com/koinephp/QueryBuilder)
-------------------------------------------------------------------------------------------------------

[](#important-notice-deprecated-in-favor-of-koinequerybuilder)

This project was deprecated in favor of [Koine/QueryBuilder](https://github.com/koinephp/QueryBuilder)

[![Build Status](https://camo.githubusercontent.com/e119a6ec4efd493d95f4cd35be3939c521dbb92e8697206ddaa1979e2fbf5e63/68747470733a2f2f7472617669732d63692e6f72672f6d6a61636f6275732f7068702d71756572792d6275696c6465722e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/mjacobus/php-query-builder)[![Coverage Status](https://camo.githubusercontent.com/cc5592c8f7b5b14bf2cc26db057d1e0f60329793a5cc1e4f761f6d69e31d7f73/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6d6a61636f6275732f7068702d71756572792d6275696c6465722f62616467652e706e67)](https://coveralls.io/r/mjacobus/php-query-builder)[![Code Climate](https://camo.githubusercontent.com/e3395715b0f8cb5e6ee062df0cc8d779f5a8c82a79b5424f6036de8e0db73dcf/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6d6a61636f6275732f7068702d71756572792d6275696c6465722e706e67)](https://codeclimate.com/github/mjacobus/php-query-builder)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/882651ca015aa1274fb76f89b4c7752bbfd656655f93f7217a1b05cc51eb7c89/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d6a61636f6275732f7068702d71756572792d6275696c6465722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mjacobus/php-query-builder/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/f7e6dd48b5e4cacf2e1bcc29c6abca8dc907906fda06e17bf513e1a0e5ac566f/68747470733a2f2f706f7365722e707567782e6f72672f7068702d6f626a656374732f71756572792d6275696c6465722f762f737461626c652e737667)](https://packagist.org/packages/php-objects/query-builder)[![Total Downloads](https://camo.githubusercontent.com/b5c3bac9a656e7bc958f71b83c3d1124c7e0616e9d7f9291418a3f55aeedd0b5/68747470733a2f2f706f7365722e707567782e6f72672f7068702d6f626a656374732f71756572792d6275696c6465722f646f776e6c6f6164732e737667)](https://packagist.org/packages/php-objects/query-builder)[![Latest Unstable Version](https://camo.githubusercontent.com/2ff434ad70702750509f733611f9bcca547d36400fd3ae7a31229cf1be9a27f3/68747470733a2f2f706f7365722e707567782e6f72672f7068702d6f626a656374732f71756572792d6275696c6465722f762f756e737461626c652e737667)](https://packagist.org/packages/php-objects/query-builder)[![License](https://camo.githubusercontent.com/38b975541cf5925338883bd99269be44ce2b3b4c8d251d612fd3ecb210cc7014/68747470733a2f2f706f7365722e707567782e6f72672f7068702d6f626a656374732f71756572792d6275696c6465722f6c6963656e73652e737667)](https://packagist.org/packages/php-objects/query-builder)

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
        "php-objects/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 = PO\QueryBuilder::factorySelect($fields);

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

// or alternatively
$select = new PO\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 = PO\QueryBuilder::insert();

// Or alternatively
$insert = new PO\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 = PO\QueryBuilder::update('users');

// or
$update = new PO\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@abc.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/mjacobus/php-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
```

Lincense
--------

[](#lincense)

[MIT](MIT-LICENSE)

Authors
-------

[](#authors)

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

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity56

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 ~7 days

Total

5

Last Release

4318d 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 (97 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/php-objects-query-builder/health.svg)](https://phpackages.com/packages/php-objects-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)
