PHPackages                             penobit/idb - 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. penobit/idb

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

penobit/idb
===========

A lightweight, expressive, framework agnostic query builder for PHP.

v1.1.1(4y ago)113MITPHP

Since Jul 12Pushed 2y ago1 watchersCompare

[ Source](https://github.com/penobit/idb)[ Packagist](https://packagist.org/packages/penobit/idb)[ Docs](https://penobit.com/sources/idb)[ GitHub Sponsors](https://github.com/penobit)[ RSS](/packages/penobit-idb/feed)WikiDiscussions main Synced 5d ago

READMEChangelog (1)Dependencies (2)Versions (4)Used By (0)

iDB Query Builder
=================

[](#idb-query-builder)

[![Build Status](https://camo.githubusercontent.com/5ffcf836298b4fd73642efef46a0d9c3a24eb03562d3a04e4b8136236e21da68/68747470733a2f2f7472617669732d63692e6f72672f70656e6f6269742f6964622e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/penobit/idb)[![Total Downloads](https://camo.githubusercontent.com/8b529436242157fe833ea5afadc3d4fcd4b55c9c53ed5c25b7c792230049dded/68747470733a2f2f706f7365722e707567782e6f72672f70656e6f6269742f6964622f646f776e6c6f616473)](https://packagist.org/packages/penobit/idb)[![Daily Downloads](https://camo.githubusercontent.com/44144501c6513bcd231d6e4df29a6b0209edccbe72a1e2d8376085a72696d581/68747470733a2f2f706f7365722e707567782e6f72672f70656e6f6269742f6964622f642f6461696c79)](https://packagist.org/packages/penobit/idb)

A lightweight, expressive, framework agnostic query builder for PHP it can also be referred as a Database Abstraction Layer. iDB supports MySQL, SQLite and PostgreSQL and it takes care of query sanitization, table prefixing and many other things with a unified API.

It has some advanced features like:

- Query Events- Nested Criteria- Sub Queries- Nested Queries- Multiple Database Connections.

The syntax is quite similar to Laravel's query builder.

Example
-------

[](#example)

```
// Make sure you have Composer's autoload file included
require 'vendor/autoload.php';

// Create a connection, once only.
$config = [
            'driver'    => 'mysql', // Db driver
            'host'      => 'localhost',
            'database'  => 'your-database',
            'username'  => 'root',
            'password'  => 'your-password',
            'charset'   => 'utf8', // Optional
            'collation' => 'utf8_unicode_ci', // Optional
            'prefix'    => 'cb_', // Table prefix, optional
            'options'   => [ // PDO constructor options, optional
                PDO::ATTR_TIMEOUT => 5,
                PDO::ATTR_EMULATE_PREPARES => false,
            ],
        ];

new \iDB\Connection('mysql', $config, 'QB');
```

**Simple Query:**

The query below returns the row where id = 3, null if no rows.

```
$row = QB::table('my_table')->find(3);
```

**Full Queries:**

```
$query = QB::table('my_table')->where('name', '=', 'Sana');

// Get result
$query->get();
```

**Query Events:**

After the code below, every time a select query occurs on `users` table, it will add this where criteria, so banned users don't get access.

```
QB::registerEvent('before-select', 'users', function($qb)
{
    $qb->where('status', '!=', 'banned');
});
```

There are many advanced options which are documented below. Sold? Let's install.

Installation
------------

[](#installation)

iDB uses [Composer](http://getcomposer.org/doc/00-intro.md#installation-nix) to make things easy.

Learn to use composer and add this to require section (in your composer.json):

"penobit/idb": "2.\*@dev"

And run:

composer update

Library on [Packagist](https://packagist.org/packages/penobit/idb).

Full Usage API
--------------

[](#full-usage-api)

### Table of Contents

[](#table-of-contents)

- [iDB Query Builder](#idb-query-builder)
    - [Example](#example)
    - [Installation](#installation)
    - [Full Usage API](#full-usage-api)
        - [Table of Contents](#table-of-contents)
    - [Connection](#connection)
        - [Alias](#alias)
        - [SQLite and PostgreSQL Config Sample](#sqlite-and-postgresql-config-sample)
    - [Query](#query)
        - [Get Easily](#get-easily)
        - [Select](#select)
            - [Multiple Selects](#multiple-selects)
            - [Select Distinct](#select-distinct)
            - [Get All](#get-all)
            - [Get First Row](#get-first-row)
            - [Get Rows Count](#get-rows-count)
        - [Where](#where)
            - [Where In](#where-in)
            - [Where Between](#where-between)
            - [Where Null](#where-null)
            - [Grouped Where](#grouped-where)
            - [JSON where](#json-where)
        - [Group By and Order By](#group-by-and-order-by)
            - [Multiple Group By](#multiple-group-by)
        - [Having](#having)
        - [Limit and Offset](#limit-and-offset)
        - [Join](#join)
            - [Multiple Join Criteria](#multiple-join-criteria)
        - [Raw Query](#raw-query)
            - [Raw Expressions](#raw-expressions)
        - [Insert](#insert)
            - [Batch Insert](#batch-insert)
            - [Insert with ON DUPLICATE KEY statement](#insert-with-on-duplicate-key-statement)
        - [Update](#update)
        - [Delete](#delete)
        - [Transactions](#transactions)
        - [Get Built Query](#get-built-query)
        - [Sub Queries and Nested Queries](#sub-queries-and-nested-queries)
        - [Get PDO Instance](#get-pdo-instance)
        - [Fetch results as objects of specified class](#fetch-results-as-objects-of-specified-class)
        - [Query Events](#query-events)
            - [Available Events](#available-events)
            - [Registering Events](#registering-events)
            - [Removing Events](#removing-events)
            - [Some Use Cases](#some-use-cases)
            - [Notes](#notes)

---

Connection
----------

[](#connection)

iDB supports three database drivers, MySQL, SQLite and PostgreSQL. You can specify the driver during connection and the associated configuration when creating a new connection. You can also create multiple connections, but you can use alias for only one connection at a time.;

```
// Make sure you have Composer's autoload file included
require 'vendor/autoload.php';

$config = array(
            'driver'    => 'mysql', // Db driver
            'host'      => 'localhost',
            'database'  => 'your-database',
            'username'  => 'root',
            'password'  => 'your-password',
            'charset'   => 'utf8', // Optional
            'collation' => 'utf8_unicode_ci', // Optional
            'prefix'    => 'cb_', // Table prefix, optional
        );

new \iDB\Connection('mysql', $config, 'QB');

// Run query
$query = QB::table('my_table')->where('name', '=', 'Sana');
```

### Alias

[](#alias)

When you create a connection:

```
new \iDB\Connection('mysql', $config, 'MyAlias');
```

`MyAlias` is the name for the class alias you want to use (like `MyAlias::table(...)`), you can use whatever name (with Namespace also, `MyNamespace\\MyClass`) you like or you may skip it if you don't need an alias. Alias gives you the ability to easily access the QueryBuilder class across your application.

When not using an alias you can instantiate the QueryBuilder handler separately, helpful for Dependency Injection and Testing.

```
$connection = new \iDB\Connection('mysql', $config);
$qb = new \iDB\QueryBuilder\QueryBuilderHandler($connection);

$query = $qb->table('my_table')->where('name', '=', 'Sana');

var_dump($query->get());
```

`$connection` here is optional, if not given it will always associate itself to the first connection, but it can be useful when you have multiple database connections.

### SQLite and PostgreSQL Config Sample

[](#sqlite-and-postgresql-config-sample)

```
new \iDB\Connection('sqlite', array(
                'driver'   => 'sqlite',
			    'database' => 'your-file.sqlite',
			    'prefix'   => 'cb_',
		    ), 'QB');
```

```
new \iDB\Connection('pgsql', array(
                    'driver'   => 'pgsql',
                    'host'     => 'localhost',
                    'database' => 'your-database',
                    'username' => 'postgres',
                    'password' => 'your-password',
                    'charset'  => 'utf8',
                    'prefix'   => 'cb_',
                    'schema'   => 'public',
                ), 'QB');
```

Query
-----

[](#query)

You **must** use `table()` method before every query, except raw `query()`. To select from multiple tables just pass an array.

```
QB::table(array('mytable1', 'mytable2'));
```

### Get Easily

[](#get-easily)

The query below returns the (first) row where id = 3, null if no rows.

```
$row = QB::table('my_table')->find(3);
```

Access your row like, `echo $row->name`. If your field name is not `id` then pass the field name as second parameter `QB::table('my_table')->find(3, 'person_id');`.

The query below returns the all rows where name = 'Sana', null if no rows.

```
$result = QB::table('my_table')->findAll('name', 'Sana');
```

### Select

[](#select)

```
$query = QB::table('my_table')->select('*');
```

#### Multiple Selects

[](#multiple-selects)

```
->select(array('mytable.myfield1', 'mytable.myfield2', 'another_table.myfield3'));
```

Using select method multiple times `select('a')->select('b')` will also select `a` and `b`. Can be useful if you want to do conditional selects (within a PHP `if`).

#### Select Distinct

[](#select-distinct)

```
->selectDistinct(array('mytable.myfield1', 'mytable.myfield2'));
```

#### Get All

[](#get-all)

Return an array.

```
$query = QB::table('my_table')->where('name', '=', 'Sana');
$result = $query->get();
```

You can loop through it like:

```
foreach ($result as $row) {
    echo $row->name;
}
```

#### Get First Row

[](#get-first-row)

```
$query = QB::table('my_table')->where('name', '=', 'Sana');
$row = $query->first();
```

Returns the first row, or null if there is no record. Using this method you can also make sure if a record exists. Access these like `echo $row->name`.

#### Get Rows Count

[](#get-rows-count)

```
$query = QB::table('my_table')->where('name', '=', 'Sana');
$query->count();
```

### Where

[](#where)

Basic syntax is `(fieldname, operator, value)`, if you give two parameters then `=` operator is assumed. So `where('name', 'penobit')` and `where('name', '=', 'penobit')` is the same.

```
QB::table('my_table')
    ->where('name', '=', 'penobit')
    ->whereNot('age', '>', 25)
    ->orWhere('type', '=', 'admin')
    ->orWhereNot('description', 'LIKE', '%query%')
    ;
```

#### Where In

[](#where-in)

```
QB::table('my_table')
    ->whereIn('name', array('penobit', 'sana'))
    ->orWhereIn('name', array('heera', 'dalim'))
    ;

QB::table('my_table')
    ->whereNotIn('name', array('heera', 'dalim'))
    ->orWhereNotIn('name', array('penobit', 'sana'))
    ;
```

#### JSON Where

[](#json-where)

Select, update or delete a row using json data of a column using "column-&gt;json\_key" syntaxt

```
QB::table('my_table')
    ->where('json_data->username', 'R8')
;

QB::table('my_table')
    ->whereNotIn('json_data->username', ['Arash'])
;
```

#### Where Between

[](#where-between)

```
QB::table('my_table')
    ->whereBetween('id', 10, 100)
    ->orWhereBetween('status', 5, 8);
```

#### Where Null

[](#where-null)

```
QB::table('my_table')
    ->whereNull('modified')
    ->orWhereNull('field2')
    ->whereNotNull('field3')
    ->orWhereNotNull('field4');
```

#### Grouped Where

[](#grouped-where)

Sometimes queries get complex, where you need grouped criteria, for example `WHERE age = 10 and (name like '%penobit%' or description LIKE '%penobit%')`.

iDB allows you to do so, you can nest as many closures as you need, like below.

```
QB::table('my_table')
            ->where('my_table.age', 10)
            ->where(function($q)
                {
                    $q->where('name', 'LIKE', '%penobit%');
                    // You can provide a closure on these wheres too, to nest further.
                    $q->orWhere('description', 'LIKE', '%penobit%');
                });
```

### Group By and Order By

[](#group-by-and-order-by)

```
$query = QB::table('my_table')->groupBy('age')->orderBy('created_at', 'ASC');
```

#### Multiple Group By

[](#multiple-group-by)

```
->groupBy(array('mytable.myfield1', 'mytable.myfield2', 'another_table.myfield3'));

->orderBy(array('mytable.myfield1', 'mytable.myfield2', 'another_table.myfield3'));
```

Using `groupBy()` or `orderBy()` methods multiple times `groupBy('a')->groupBy('b')` will also group by first `a` and than `b`. Can be useful if you want to do conditional grouping (within a PHP `if`). Same applies to `orderBy()`.

### Having

[](#having)

```
->having('total_count', '>', 2)
->orHaving('type', '=', 'admin');
```

### Limit and Offset

[](#limit-and-offset)

```
->limit(30);

->offset(10);
```

### Join

[](#join)

```
QB::table('my_table')
    ->join('another_table', 'another_table.person_id', '=', 'my_table.id')
```

Available methods,

- join() or innerJoin
- leftJoin()
- rightJoin()

If you need `FULL OUTER` join or any other join, just pass it as 5th parameter of `join` method.

```
->join('another_table', 'another_table.person_id', '=', 'my_table.id', 'FULL OUTER')
```

#### Multiple Join Criteria

[](#multiple-join-criteria)

If you need more than one criterion to join a table then pass a closure as second parameter.

```
->join('another_table', function($table)
    {
        $table->on('another_table.person_id', '=', 'my_table.id');
        $table->on('another_table.person_id2', '=', 'my_table.id2');
        $table->orOn('another_table.age', '>', QB::raw(1));
    })
```

### Raw Query

[](#raw-query)

You can always use raw queries if you need,

```
$query = QB::query('select * from cb_my_table where age = 12');

var_dump($query->get());
```

You can also pass your bindings

```
QB::query('select * from cb_my_table where age = ? and name = ?', array(10, 'penobit'));
```

#### Raw Expressions

[](#raw-expressions)

When you wrap an expression with `raw()` method, iDB doesn't try to sanitize these.

```
QB::table('my_table')
            ->select(QB::raw('count(cb_my_table.id) as tot'))
            ->where('value', '=', 'Ifrah')
            ->where(QB::raw('DATE(?)', 'now'))
```

---

**NOTE:** Queries that run through `query()` method are not sanitized until you pass all values through bindings. Queries that run through `raw()` method are not sanitized either, you have to do it yourself. And of course these don't add table prefix too, but you can use the `addTablePrefix()` method.

### Insert

[](#insert)

```
$data = array(
    'name' => 'Sana',
    'description' => 'Blah'
);
$insertId = QB::table('my_table')->insert($data);
```

`insert()` method returns the insert id.

#### Batch Insert

[](#batch-insert)

```
$data = array(
    array(
        'name'        => 'Sana',
        'description' => 'Blah'
    ),
    array(
        'name'        => 'Penobit',
        'description' => 'Blah'
    ),
);
$insertIds = QB::table('my_table')->insert($data);
```

In case of batch insert, it will return an array of insert ids.

#### Insert with ON DUPLICATE KEY statement

[](#insert-with-on-duplicate-key-statement)

```
$data = array(
    'name'    => 'Sana',
    'counter' => 1
);
$dataUpdate = array(
    'name'    => 'Sana',
    'counter' => 2
);
$insertId = QB::table('my_table')->onDuplicateKeyUpdate($dataUpdate)->insert($data);
```

### Update

[](#update)

```
$data = array(
    'name'        => 'Sana',
    'description' => 'Blah'
);

QB::table('my_table')->where('id', 5)->update($data);
```

Will update the name field to Sana and description field to Blah where id = 5.

### Delete

[](#delete)

```
QB::table('my_table')->where('id', '>', 5)->delete();
```

Will delete all the rows where id is greater than 5.

### Transactions

[](#transactions)

iDB has the ability to run database "transactions", in which all database changes are not saved until committed. That way, if something goes wrong or differently then you intend, the database changes are not saved and no changes are made.

Here's a basic transaction:

```
QB::transaction(function ($qb) {
    $qb->table('my_table')->insert(array(
        'name' => 'Test',
        'url' => 'example.com'
    ));

    $qb->table('my_table')->insert(array(
        'name' => 'Test2',
        'url' => 'example.com'
    ));
});
```

If this were to cause any errors (such as a duplicate name or some other such error), neither data set would show up in the database. If not, the changes would be successfully saved.

If you wish to manually commit or rollback your changes, you can use the `commit()` and `rollback()` methods accordingly:

```
QB::transaction(function ($qb) {
    $qb->table('my_table')->insert(array(/* data... */));

    $qb->commit(); // to commit the changes (data would be saved)
    $qb->rollback(); // to rollback the changes (data would be rejected)
});
```

### Get Built Query

[](#get-built-query)

Sometimes you may need to get the query string, its possible.

```
$query = QB::table('my_table')->where('id', '=', 3);
$queryObj = $query->getQuery();
```

`getQuery()` will return a query object, from this you can get sql, bindings or raw sql.

```
$queryObj->getSql();
// Returns: SELECT * FROM my_table where `id` = ?

```

```
$queryObj->getBindings();
// Returns: array(3)
```

```
$queryObj->getRawSql();
// Returns: SELECT * FROM my_table where `id` = 3
```

### Sub Queries and Nested Queries

[](#sub-queries-and-nested-queries)

Rarely but you may need to do sub queries or nested queries. iDB is powerful enough to do this for you. You can create different query objects and use the `QB::subQuery()` method.

```
$subQuery = QB::table('person_details')->select('details')->where('person_id', '=', 3);

$query = QB::table('my_table')
            ->select('my_table.*')
            ->select(QB::subQuery($subQuery, 'table_alias1'));

$nestedQuery = QB::table(QB::subQuery($query, 'table_alias2'))->select('*');
$nestedQuery->get();
```

This will produce a query like this:

```
SELECT * FROM (SELECT `cb_my_table`.*, (SELECT `details` FROM `cb_person_details` WHERE `person_id` = 3) as table_alias1 FROM `cb_my_table`) as table_alias2

```

**NOTE:** iDB doesn't use bindings for sub queries and nested queries. It quotes values with PDO's `quote()` method.

### Get PDO Instance

[](#get-pdo-instance)

If you need to get the PDO instance you can do so.

```
QB::pdo();
```

### Fetch results as objects of specified class

[](#fetch-results-as-objects-of-specified-class)

Simply call `asObject` query's method.

```
QB::table('my_table')->asObject('SomeClass', array('ctor', 'args'))->first();
```

Furthermore, you may fine-tune fetching mode by calling `setFetchMode` method.

```
QB::table('my_table')->setFetchMode(PDO::FETCH_COLUMN|PDO::FETCH_UNIQUE)->get();
```

### Query Events

[](#query-events)

iDB comes with powerful query events to supercharge your application. These events are like database triggers, you can perform some actions when an event occurs, for example you can hook `after-delete` event of a table and delete related data from another table.

#### Available Events

[](#available-events)

- before-select
- after-select
- before-insert
- after-insert
- before-update
- after-update
- before-delete
- after-delete

#### Registering Events

[](#registering-events)

```
QB::registerEvent('before-select', 'users', function($qb)
{
    $qb->where('status', '!=', 'banned');
});
```

Now every time a select query occurs on `users` table, it will add this where criteria, so banned users don't get access.

The syntax is `registerEvent('event type', 'table name', action in a closure)`.

If you want the event to be performed when **any table is being queried**, provide `':any'` as table name.

**Other examples:**

After inserting data into `my_table`, details will be inserted into another table

```
QB::registerEvent('after-insert', 'my_table', function($queryBuilder, $insertId)
{
    $data = array('person_id' => $insertId, 'details' => 'Meh', 'age' => 5);
    $queryBuilder->table('person_details')->insert($data);
});
```

Whenever data is inserted into `person_details` table, set the timestamp field `created_at`, so we don't have to specify it everywhere:

```
QB::registerEvent('after-insert', 'person_details', function($queryBuilder, $insertId)
{
    $queryBuilder->table('person_details')->where('id', $insertId)->update(array('created_at' => date('Y-m-d H:i:s')));
});
```

After deleting from `my_table` delete the relations:

```
QB::registerEvent('after-delete', 'my_table', function($queryBuilder, $queryObject)
{
    $bindings = $queryObject->getBindings();
    $queryBuilder->table('person_details')->where('person_id', $binding[0])->delete();
});
```

iDB passes the current instance of query builder as first parameter of your closure so you can build queries with this object, you can do anything like usual query builder (`QB`).

If something other than `null` is returned from the `before-*` query handler, the value will be result of execution and DB will not be actually queried (and thus, corresponding `after-*` handler will not be called either).

Only on `after-*` events you get three parameters: **first** is the query builder, **third** is the execution time as float and **the second** varies:

- On `after-select` you get the `results` obtained from `select`.
- On `after-insert` you get the insert id (or array of ids in case of batch insert)
- On `after-delete` you get the [query object](#get-built-query) (same as what you get from `getQuery()`), from it you can get SQL and Bindings.
- On `after-update` you get the [query object](#get-built-query) like `after-delete`.

#### Removing Events

[](#removing-events)

```
QB::removeEvent('event-name', 'table-name');
```

#### Some Use Cases

[](#some-use-cases)

Here are some cases where Query Events can be extremely helpful:

- Restrict banned users.
- Get only `deleted = 0` records.
- Implement caching of all queries.
- Trigger user notification after every entry.
- Delete relationship data after a delete query.
- Insert relationship data after an insert query.
- Keep records of modification after each update query.
- Add/edit created\_at and updated \_at data after each entry.

#### Notes

[](#notes)

- Query Events are set as per connection basis so multiple database connection don't create any problem, and creating new query builder instance preserves your events.
- Query Events go recursively, for example after inserting into `table_a` your event inserts into `table_b`, now you can have another event registered with `table_b` which inserts into `table_c`.
- Of course Query Events don't work with raw queries.

---

If you find any typo then please edit and send a pull request.

© 2020 [Penobit](http://penobit.com/). Licensed under MIT license.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity53

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

Total

3

Last Release

1691d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8cbcaf2d9ebcdc5282f9dde8aeb9e6cff2d8bd8735b21669fb242e3a47dd7642?d=identicon)[penobit](/maintainers/penobit)

---

Top Contributors

[![penobit](https://avatars.githubusercontent.com/u/60283818?v=4)](https://github.com/penobit "penobit (6 commits)")

---

Tags

databasemysqlsqlitepostgresqlsqlquery builderpenobit

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/penobit-idb/health.svg)

```
[![Health](https://phpackages.com/badges/penobit-idb/health.svg)](https://phpackages.com/packages/penobit-idb)
```

###  Alternatives

[doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.

9.7k578.4M5.6k](/packages/doctrine-dbal)[usmanhalalit/pixie

A lightweight, expressive, framework agnostic query builder for PHP.

6872.2M15](/packages/usmanhalalit-pixie)[cycle/orm

PHP DataMapper ORM and Data Modelling Engine

1.3k835.4k65](/packages/cycle-orm)[cycle/database

DBAL, schema introspection, migration and pagination

64690.9k31](/packages/cycle-database)[pecee/pixie

Lightweight, fast query-builder for PHP based on Laravel Eloquent but with less overhead.

4128.7k8](/packages/pecee-pixie)[opis/database

A database abstraction layer over PDO, that provides a powerful and intuitive query builder, bundled with an easy to use schema builder

10184.2k3](/packages/opis-database)

PHPackages © 2026

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