PHPackages                             pecee/pixie - 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. pecee/pixie

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

pecee/pixie
===========

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

4.16.3(2y ago)4128.7k—6.3%13[7 issues](https://github.com/skipperbent/pecee-pixie/issues)[1 PRs](https://github.com/skipperbent/pecee-pixie/pulls)7MITPHPPHP &gt;=7.1CI failing

Since Feb 15Pushed 2y ago3 watchersCompare

[ Source](https://github.com/skipperbent/pecee-pixie)[ Packagist](https://packagist.org/packages/pecee/pixie)[ Docs](https://github.com/skipperbent/pecee-pixie)[ RSS](/packages/pecee-pixie/feed)WikiDiscussions v4-release Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (68)Used By (7)

pecee/pixie: Advanced lightweight querybuilder
==============================================

[](#peceepixie-advanced-lightweight-querybuilder)

A lightweight, expressive, framework agnostic query builder for PHP it can also be referred as a Database Abstraction Layer. Pixie supports MySQL, MS-SQL, SQLite and PostgreSQL will handle all your query sanitization, table alias, unions among many other things, with a unified API.

The syntax is similar to Laravel's query builder "Eloquent", but with less overhead.

This library is stable, maintained and are used by sites around the world (check the [credits](#credits)).

**Requirements:**

- PHP version 7.1 or higher is required for pecee-pixie version 4.x and above (versions prior to 4.x are available [here](https://github.com/skipperbent/pixie)).
- PDO extension enabled.

**Features:**

- Improved sub-queries.
- Custom prefix/aliases for tables (prefix.`table`).
- Support for not defining table and/or removing defined table.
- Better handling of `Raw` objects in `where` statements.
- Union queries.
- Better connection handling.
- Performance optimisations.
- Tons of bug fixes.
- Much more...

**Including all the original features like:**

- Query events
- Nested criteria
- Sub queries
- Nested queries
- Multiple database connections.

Most importantly this project is used on many live-sites and maintained.

### Examples

[](#examples)

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

// Create a connection, once only.
$config =
[
    // Name of database driver or IConnectionAdapter class
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'your-database',
    'username'  => 'root',
    'password'  => 'your-password',

    // Optional
    'charset'   => 'utf8',

    // Optional
    'collation' => 'utf8_unicode_ci',

    // Table prefix, optional
    'prefix'    => 'cb_',

    // PDO constructor options, optional
    'options'   => [
        PDO::ATTR_TIMEOUT => 5,
        PDO::ATTR_EMULATE_PREPARES => false,
    ],
];

$queryBuilder = (new \Pecee\Pixie\Connection('mysql', $config))->getQueryBuilder();
```

**Simple query:**

Get user with the id of `3`. Returns `null` when no match.

```
$user = $queryBuilder
            ->table('users')
            ->find(3);
```

**Full queries:**

Get all users with blue or red hair.

```
$users = $queryBuilder
            ->table('users')
            ->where('hair_color', '=', 'blue')
            ->orWhere('hair_color', '=', 'red')
            ->get();
```

---

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Feedback and development](#feedback-and-development)
    - [Issues guidelines](#issues-guidelines)
    - [Contribution and development guidelines](#contribution-and-development-guidelines)
- [Connecting to the database](#connecting-to-the-database)
    - [SQLite and PostgreSQL config example](#sqlite-and-postgresql-config-example)
- [**Select**](#select)
    - [Table alias](#table-alias)
    - [Get easily](#get-easily)
    - [Multiple selects](#multiple-selects)
    - [Select distinct](#select-distinct)
    - [Select from query](#select-from-query)
    - [Select single field](#select-single-field)
    - [Select multiple fields](#select-multiple-fields)
    - [Get all](#get-all)
    - [Get first row](#get-first-row)
    - [**Aggregate methods**](#aggregate-methods)
        - [Getting the row count](#getting-the-row-count)
        - [Getting the sum](#getting-the-sum)
        - [Getting the average](#getting-the-average)
        - [Getting the minimum](#getting-the-minimum)
        - [Getting the maximum](#getting-the-maximum)
    - [Selects with sub-queries](#select-with-sub-queries)
- [**Where**](#where)
    - [Where in](#where-in)
    - [Where between](#where-between)
    - [Where null](#where-null)
    - [Grouped where](#grouped-where)
- [Group- and order by](#group--and-order-by)
- [Having](#having)
- [Limit and offset](#limit-and-offset)
- [Join](#join)
    - [Join USING syntax](#join-using-syntax)
    - [Multiple join criteria](#multiple-join-criteria)
- [Unions](#unions)
- [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 raw query](#get-built-query)
    - [Get QueryObject from last executed query](#get-queryobject-from-last-executed-query)
- [Sub-queries and nested queries](#sub-queries-and-nested-queries)
- [Getting the PDO instance](#getting-the-pdo-instance)
- [Fetch results as objects of specified class](#fetch-results-as-objects-of-specified-class)
- [Advanced](#advanced)
    - [Enable query-overwriting](#enable-query-overwriting)
- [Query events](#query-events)
    - [Available event](#available-event)
    - [Registering event](#registering-event)
    - [Removing event](#removing-event)
    - [Use cases](#use-cases)
    - [Notes](#notes)
- [Exceptions](#exceptions)
    - [Getting sql-query from exceptions](#getting-sql-query-from-exceptions)
- [Credits](#credits)

---

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

[](#installation)

Pixie 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):

```
composer install pecee/pixie

```

Feedback and development
------------------------

[](#feedback-and-development)

If you are missing a feature, experience problems or have ideas or feedback that you want us to hear, please feel free to create an issue.

#### Issues guidelines

[](#issues-guidelines)

- Please be as detailed as possible in the description when creating a new issue. This will help others to more easily understand- and solve your issue. For example: if you are experiencing issues, you should provide the necessary steps to reproduce the error within your description.
- We love to hear out any ideas or feedback to the library.

[Create a new issue here](https://github.com/skipperbent/pecee-pixie/issues)

#### Contribution and development guidelines

[](#contribution-and-development-guidelines)

- Please try to follow the PSR-2 codestyle guidelines.
- Please create your pull requests to the development base that matches the version number you want to change. For example when pushing changes to version 3, the pull request should use the `v3-development` base/branch.
- Create detailed descriptions for your commits, as these will be used in the changelog for new releases.
- When changing existing functionality, please ensure that the unit-tests working.
- When adding new stuff, please remember to add new unit-tests for the functionality.

Connecting to the database
--------------------------

[](#connecting-to-the-database)

Pixie 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 = [
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'your-database',
    'username'  => 'root',
    'password'  => 'your-password',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
];

// Creates new connection
$connection = new \Pecee\Pixie\Connection('mysql', $config);

// Get the query-builder object which will initialize the database connection
$queryBuilder = $connection->getQueryBuilder();

// Run query
$person = $queryBuilder
            ->table('persons')
            ->where('name', '=', 'Bobby')
            ->first();
```

`$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.

**NOTE:**Calling the `getQueryBuilder` method will automatically make a connection to the database, if none has already established. If you want to access the `Pdo` instance directly from the `Connection` class, make sure to call `$connection->connect();` to establish a connection to the database.

### SQLite and PostgreSQL config example

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

The example below is for use with sqlite-databases.

```
$queryBuilder = new \Pecee\Pixie\Connection('sqlite', [
    'driver'   => 'sqlite',
    'database' => 'your-file.sqlite',
    'prefix'   => 'cb_',
]);
```

The example below is for pgsql databases.

```
$queryBuilder = new \Pecee\Pixie\Connection('pgsql', [
    'driver'   => 'pgsql',
    'host'     => 'localhost',
    'database' => 'your-database',
    'username' => 'postgres',
    'password' => 'your-password',
    'charset'  => 'utf8',
    'prefix'   => 'cb_',
    'schema'   => 'public',
]);
```

Select
------

[](#select)

It is recommend to use `table()` method before every query, except raw `query()`. To select from multiple tables just pass an array.

However this is not required.

```
$queryBuilder->table(array('mytable1', 'mytable2'));
```

#### Table alias

[](#table-alias)

You can easily set the table alias by using

```
$queryBuilder
    ->table(['table1' => 'foo1'])
    ->join('table2', 'table2.person_id', '=', 'foo1.id');
```

You can change the alias anytime by using:

```
$queryBuilder->alias('foo1', 'table1');

// Simplified way...

$queryBuilder->table('table1')->alias('foo1');
```

Output:

```
SELECT *
FROM `table1` AS `foo1`
INNER JOIN `cb_table2` ON `cb_table2`.`person_id` = `cb_foo1`.`id`
```

**NOTE:**You can always remove a table from a query by calling the `table` method with no arguments like this `$queryBuilder->table()`.

#### Get easily

[](#get-easily)

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

```
$row = $queryBuilder
            ->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 `$queryBuilder->table('my_table')->find(3, 'person_id');`.

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

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

#### Multiple selects

[](#multiple-selects)

```
$queryBuilder
    ->select(
        [
            '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)

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

#### Select from query

[](#select-from-query)

You can easily select items from another query by using

```
$subQuery = $queryBuilder->table('person');
$builder = $queryBuilder->table($queryBuilder->subQuery($subQuery))->where('id', '=', 2);
```

Will produce the following output:

```
SELECT * FROM (SELECT * FROM `person`) WHERE `id` = 2
```

#### Select single field

[](#select-single-field)

```
$queryBuilder->table('my_table')->select('*');
```

#### Select multiple fields

[](#select-multiple-fields)

```
$queryBuilder->table('my_table')->select(array('field1', 'field2'));
```

#### Get all

[](#get-all)

Returns an array.

```
$results = $queryBuilder
                ->table('my_table')
                ->where('name', '=', 'Sana')
                ->get();
```

You can loop through it like:

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

#### Get first row

[](#get-first-row)

```
$row = $queryBuilder
            ->table('my_table')
            ->where('name', '=', 'Sana')
            ->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`.

#### Aggregate methods

[](#aggregate-methods)

##### Getting the row count

[](#getting-the-row-count)

This will return the count for the entire number of rows in the result.

The default behavior will count `*` (all) fields. You can specify a custom field by changing the `field` parameter.

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

##### Getting the sum

[](#getting-the-sum)

This will return the sum for a field in the entire number of rows in the result.

```
$queryBuilder
    ->table('my_table')
    ->where('name', '=', 'Sana')
    ->sum('views');
```

##### Getting the average

[](#getting-the-average)

This will return the average for a field in the entire number of rows in the result.

```
$queryBuilder
    ->table('my_table')
    ->where('name', '=', 'Sana')
    ->average('views');
```

##### Getting the minimum

[](#getting-the-minimum)

This will return the minimum for a field in the entire number of rows in the result.

```
$queryBuilder
    ->table('my_table')
    ->where('name', '=', 'Sana')
    ->min('views');
```

##### Getting the maximum

[](#getting-the-maximum)

This will return the average for a field in the entire number of rows in the result.

```
$queryBuilder
    ->table('my_table')
    ->where('name', '=', 'Sana')
    ->max('views');
```

#### Select with sub-queries

[](#select-with-sub-queries)

```
// Creates the first sub-query

$subQuery1 =
    $queryBuilder
        ->table('mail')
        ->select(
            $queryBuilder->raw('COUNT(*)')
        );

// Create the second sub-query

$subQuery2 =
    $queryBuilder
        ->table('event_message')
        ->select(
            $queryBuilder->raw('COUNT(*)')
        );

// Executes the query which uses the subqueries as fields
$count =
    $queryBuilder
        ->select(
            $queryBuilder->subQuery($subQuery1, 'row1'),
            $queryBuilder->subQuery($subQuery2, 'row2')
        )
        ->first();
```

The example above will output a SQL-query like this:

```
SELECT
  (SELECT COUNT(*)
   FROM `cb_mail`) AS row1,

  (SELECT COUNT(*)
   FROM `cb_event_message`) AS row2
LIMIT 1
```

You can also easily create a subjquery within the `where` statement:

```
$queryBuilder->where($queryBuilder->subQuery($subQuery), '!=', 'value');
```

### Where

[](#where)

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

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

#### Where in

[](#where-in)

```
$queryBuilder
    ->table('my_table')
    ->whereIn('name', array('usman', 'sana'))
    ->orWhereIn('name', array('heera', 'dalim'));

$queryBuilder
    ->table('my_table')
    ->whereNotIn('name', array('heera', 'dalim'))
    ->orWhereNotIn('name', array('usman', 'sana'));
```

#### Where between

[](#where-between)

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

#### Where null

[](#where-null)

```
$queryBuilder
    ->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 '%usman%' or description LIKE '%usman%')`.

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

```
$queryBuilder
    ->table('my_table')
    ->where('my_table.age', 10)
    ->where(function(QueryBuilderHandler $qb) {
        $qb->where('name', 'LIKE', '%pecee%');

        // You can provide a closure on these wheres too, to nest further.
        $qb->orWhere('description', 'LIKE', '%usman%');
    });
```

### Group- and order by

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

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

#### Multiple group by

[](#multiple-group-by)

```
$queryBuilder
    ->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)

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

### Limit and offset

[](#limit-and-offset)

```
$queryBuilder
    ->limit(30);
    ->offset(10);
```

### Join

[](#join)

```
$queryBuilder
    ->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.

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

#### Join USING syntax

[](#join-using-syntax)

The `JOIN USING` syntax allows you to easily map two identical identifiers to one, which can be helpful on large queries.

Example:

```
$queryBuilder
    ->table('user')
    ->join('user_data', 'user_data.user_id', '=', 'user.user_id');
```

Can be simplified to:

```
$queryBuilder
    ->table('user')
    ->joinUsing('user_data', 'user_id');
```

#### Multiple join criteria

[](#multiple-join-criteria)

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

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

### Unions

[](#unions)

You can easily create unions by calling the `union` method on the `QueryBuilderHandler`.

Example:

```
$firstQuery =
    $queryBuilder
    ->table('people')
    ->whereNull('email');

$secondQuery =
    $queryBuilder
    ->table('people')
    ->where('hair_color', '=', 'green')
    ->union($firstQuery);

$thirdQuery =
    $queryBuilder
    ->table('people')
    ->where('gender', '=', 'male')
    ->union($secondQuery);

$items = $thirdQuery->get();
```

The example above will create a sql-statement similar to this:

```
(
	SELECT *
	FROM
		`cb_people`
	WHERE
		`gender` = 'male'
)
UNION
(
	SELECT *
	FROM
		`cb_people`
	WHERE
		`email`
	IS NULL
)
UNION
(
	SELECT *
	FROM
		`cb_people`
	WHERE
		`hair_color` = 'green'
)
```

### Raw query

[](#raw-query)

You can always perform raw queries, if needed.

```
$query = $queryBuilder->query('SELECT * FROM persons WHERE age = 12');

$kids = $query->get();
```

You can also pass custom bindings

```
$queryBuilder
    ->query('SELECT * FROM persons WHERE age = ? AND name = ?', array(10, 'usman'));
```

#### Raw expressions

[](#raw-expressions)

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

```
$queryBuilder
    ->table('my_table')
    ->select($queryBuilder->raw('count(cb_my_table.id) as tot'))
    ->where('value', '=', 'Ifrah')
    ->where($queryBuilder->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 = [
    'name' => 'Sana',
    'description' => 'Blah'
];

$insertId = $queryBuilder
                ->table('my_table')
                ->insert($data);
```

`insert()` method returns the insert id.

#### Batch insert

[](#batch-insert)

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

$insertIds = $queryBuilder
                ->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 = [
    'name'    => 'Sana',
    'counter' => 1
];

$dataUpdate = [
    'name'    => 'Sana',
    'counter' => 2
];

$insertId =
    $queryBuilder
        ->table('my_table')
        ->onDuplicateKeyUpdate($dataUpdate)
        ->insert($data);
```

### Update

[](#update)

```
$data = [
    'name'        => 'Sana',
    'description' => 'Blah'
];

$queryBuilder
    ->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)

```
$queryBuilder
    ->table('my_table')
    ->where('id', '>', 5)
    ->delete();
```

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

### Transactions

[](#transactions)

Pixie 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:

```
$queryBuilder
    ->transaction(function (Transaction $transaction) {

        $transaction
            ->table('my_table')
            ->insert(array(
                'name' => 'Test',
                'url' => 'example.com'
            );

        $transaction
            ->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:

```
$queryBuilder
    ->transaction(function (Transaction $transaction)
        {
            $transaction
                ->table('my_table')
                ->insert($data);

            // Commit changes (data will be saved)
            $transaction->commit();

            // Rollback changes (data would be rejected)
            $transaction->rollback();
        }
    );
```

Transactions will automatically be used when inserting multiple records. For example:

```
$queryBuilder->table('people')->insert([
    [
        'name' => 'Simon',
        'age' => 12,
        'awesome' => true,
        'nickname' => 'ponylover94',
    ],
    [
        'name' => 'Peter',
        'age' => 40,
        'awesome' => false,
        'nickname' => null,
    ],
    [
        'name' => 'Bobby',
        'age' => 20,
        'awesome' => true,
        'nickname' => 'peter',
    ],
]);
```

### Get raw query

[](#get-raw-query)

Sometimes you may need to get the query string, it's possible.

```
$queryHandler =
    $queryBuilder
        ->table('my_table')
        ->where('id', '=', 3)
        ->getQuery();
```

`getQuery()` will return a `QueryBuilderHandler` object.

You can use this to get the SQL, bindings or raw SQL.

```
$queryHandler->getSql();
```

Calling `getSql()` will return the SQL-query without any processing.

```
SELECT * FROM my_table where `id` = ?
```

You can easily get any bindings on the query by calling the `getBindings()`method.

**Example:**

```
$queryHandler->getBindings();
```

You can also get the raw SQL-query directly by calling the `getRawSql()` method.

**Example:**

```
$queryHandler->getRawSql();
```

Calling the `getRawSql()` method will return a query including bindings like this.

```
SELECT * FROM my_table where `id` = 3
```

#### Get QueryObject from last executed query

[](#get-queryobject-from-last-executed-query)

You can also retrieve the query-object from the last executed query.

**Example:**

```
$queryString = $queryBuilder->getLastQuery()->getRawSql();
```

### Sub-queries and nested queries

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

Rarely but you may need to do sub queries or nested queries. Pixie is powerful enough to do this for you. You can create different query objects and use the `$queryBuilder->subQuery()` method.

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

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

$nestedQuery =
    $queryBuilder
        ->table(
            $queryBuilder->subQuery($query, 'table_alias2')
        )
        ->select('*');

// Execute query
$result = $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:**

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

### Getting the PDO instance

[](#getting-the-pdo-instance)

If you need the `\PDO` instance, you can easily get it by calling:

```
$queryBuilder->pdo();
```

If you want to get the `Connection` object you can do so like this:

```
$connection = $queryBuilder->getConnection();
```

### Fetch results as objects of specified class

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

Simply call `asObject` query's method.

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

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

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

### Advanced

[](#advanced)

#### Enable query-overwriting

[](#enable-query-overwriting)

If enabled calling from, select etc. will overwrite any existing values from previous calls in query.

You can enable or disable query-overwriting by calling the `setOverwriteEnabled` method on the `QueryBuilderHandler` object.

The feature is disabled as default.

**Example:**

```
$queryBuilder
    ->setOverwriteEnabled(true);
```

If you want this feature to be enabled on all `QueryBuilderHandler` object as default, you can add the following setting to the connection configuration:

```
$adapterConfig = [
    'query_overwriting' => false,
];
```

### Query events

[](#query-events)

Pixie 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)

Event constantEvent value/nameDescription`EventHandler::EVENT_BEFORE_ALL``before-*`Event-type that fires before each query.`EventHandler::EVENT_AFTER_ALL``after-*`Event-type that fires after each query.`EventHandler::EVENT_BEFORE_QUERY``before-query`Event-type that fires before a raw query is executed.`EventHandler::EVENT_AFTER_QUERY``after-query`Event-type that fires after a raw query is executed`EventHandler::EVENT_BEFORE_SELECT``before-select`Event-type that fires before select query.`EventHandler::EVENT_AFTER_SELECT``after-select`Event-type that fires after insert query.`EventHandler::EVENT_BEFORE_INSERT``before-insert`Event-type that fires before insert query`EventHandler::EVENT_AFTER_INSERT``after-insert`Event-type that fires after insert query.`EventHandler::EVENT_BEFORE_UPDATE``before-update`Event-type that fires before update query.`EventHandler::EVENT_AFTER_UPDATE``after-update`Event-type that fires after update query.`EventHandler::EVENT_BEFORE_DELETE``before-delete`Event-type that fires before delete query.`EventHandler::EVENT_AFTER_DELETE``after-delete`Event-type that fires after delete query.#### Registering event

[](#registering-event)

You can easily register a new event either by using the `registerEvent` method on either the `QueryBuilderHandler`, `Connection` or `EventHandler` class.

The event needs a custom callback function with a `EventArguments` object as parameters.

**Examples:**

```
$queryBuilder->registerEvent(EventHandler::EVENT_BEFORE_SELECT, function(EventArguments $arguments)
{
    $arguments
        ->getQueryBuilder()
        ->where('status', '!=', 'banned');
}, 'users');
```

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', action in a closure, 'table name')`.

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

```
$queryBuilder->registerEvent(EventHandler::EVENT_AFTER_INSERT, function(EventArguments $arguments)
{
    $arguments
        ->getQueryBuilder()
        ->table('person_details')->insert(array(
        'person_id' => $insertId,
        'details' => 'Meh',
        'age' => 5
    ));
}, 'my_table');
```

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

```
$queryBuilder->registerEvent(EventHandler::EVENT_AFTER_INSERT, function(EventArguments $arguments)
{
    $arguments
        ->getQueryBuilder()
        ->table('person_details')
        ->where('id', $insertId)
        ->update([
            'created_at' => date('Y-m-d H:i:s')
        ]);
}, 'person_details');
```

After deleting from `my_table` delete the relations:

```
$queryBuilder->registerEvent(EventHandler::EVENT_AFTER_DELETE, function(EventArguments $arguments)
{
    $bindings = $arguments->getQuery()->getBindings();

    $arguments
        ->getQueryBuilder()
        ->table('person_details')
        ->where('person_id', $binding[0])
        ->delete();
}, 'my_table');
```

Pixie 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 ether).

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-query` fires after a raw query has been executed.
- 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 event

[](#removing-event)

```
$queryBuilder->removeEvent($event, $table = null);
```

#### Use cases

[](#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.

### Exceptions

[](#exceptions)

This is a list over exceptions thrown by pecee-pixie.

All exceptions inherit from the base `Exception` class.

Exception name`ColumnNotFoundException``ConnectionException``DuplicateColumnException``DuplicateEntryException``ForeignKeyException``NotNullException``TableNotFoundException``Exception`#### Getting sql-query from exceptions

[](#getting-sql-query-from-exceptions)

If an error occurs and you want to debug your query - you can easily do so as all exceptions thrown by Pixie will contain the last executed query.

You can retrieve the `QueryObject` by calling

```
$sql = $exception->getQueryObject()->getRawSql();
```

Credits
-------

[](#credits)

This project is based on the original [Pixie project](https://github.com/usmanhalalit/pixie) by the incredible talented usmanhalalit.

Thanks to all the people that have contributed and the users enjoying our library.

Here's some of our references:

- [Holla.dk](https://holla.dk)
- [Dscuz.com](https://dscuz.com)
- [NinjaImg.com](https://ninjaimg.com)
- [BookAndBegin.com](https://bookandbegin.com)

---

Licence
-------

[](#licence)

Licensed under the MIT licence.

### The MIT License (MIT)

[](#the-mit-license-mit)

Copyright (c) 2016 Simon Sessingø / pecee-pixie

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity40

Moderate usage in the ecosystem

Community31

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 69.7% 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 ~42 days

Recently: every ~58 days

Total

62

Last Release

803d ago

Major Versions

3.5.1 → 4.1.12017-12-11

3.5.2 → 4.2.02018-01-16

3.6.0 → 4.2.12018-01-16

3.6.1 → 4.2.22018-01-16

3.6.4 → 4.2.52018-01-16

PHP version history (6 changes)3.0.0PHP &gt;=5.4.0

3.4.0PHP &gt;=5.5.0

3.4.2PHP &gt;=5.6

4.0.0PHP &gt;=7.0

4.6.0PHP &gt;=7.2

4.7.0PHP &gt;=7.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/2dab716ac4ac16ea3e01400efe43853b43821b33b8c601c2c772689d58cebf3c?d=identicon)[sessingo](/maintainers/sessingo)

---

Top Contributors

[![skipperbent](https://avatars.githubusercontent.com/u/634762?v=4)](https://github.com/skipperbent "skipperbent (274 commits)")[![mrcnpdlk](https://avatars.githubusercontent.com/u/22945316?v=4)](https://github.com/mrcnpdlk "mrcnpdlk (50 commits)")[![neoascetic](https://avatars.githubusercontent.com/u/725836?v=4)](https://github.com/neoascetic "neoascetic (19 commits)")[![krp-kp](https://avatars.githubusercontent.com/u/1413296?v=4)](https://github.com/krp-kp "krp-kp (7 commits)")[![usmanhalalit](https://avatars.githubusercontent.com/u/981039?v=4)](https://github.com/usmanhalalit "usmanhalalit (7 commits)")[![advicente](https://avatars.githubusercontent.com/u/7566606?v=4)](https://github.com/advicente "advicente (7 commits)")[![acburdine](https://avatars.githubusercontent.com/u/5167581?v=4)](https://github.com/acburdine "acburdine (6 commits)")[![mrjgreen](https://avatars.githubusercontent.com/u/2183391?v=4)](https://github.com/mrjgreen "mrjgreen (6 commits)")[![marc937](https://avatars.githubusercontent.com/u/4545014?v=4)](https://github.com/marc937 "marc937 (4 commits)")[![Ascron](https://avatars.githubusercontent.com/u/628110?v=4)](https://github.com/Ascron "Ascron (2 commits)")[![ad3n](https://avatars.githubusercontent.com/u/7464920?v=4)](https://github.com/ad3n "ad3n (2 commits)")[![beaucox](https://avatars.githubusercontent.com/u/5628107?v=4)](https://github.com/beaucox "beaucox (2 commits)")[![TCB13](https://avatars.githubusercontent.com/u/6315832?v=4)](https://github.com/TCB13 "TCB13 (1 commits)")[![ToToDoDo](https://avatars.githubusercontent.com/u/1905256?v=4)](https://github.com/ToToDoDo "ToToDoDo (1 commits)")[![Gurakuq](https://avatars.githubusercontent.com/u/10980212?v=4)](https://github.com/Gurakuq "Gurakuq (1 commits)")[![Kemekaze](https://avatars.githubusercontent.com/u/5463135?v=4)](https://github.com/Kemekaze "Kemekaze (1 commits)")[![lakesta](https://avatars.githubusercontent.com/u/916380?v=4)](https://github.com/lakesta "lakesta (1 commits)")[![griffithben](https://avatars.githubusercontent.com/u/632330?v=4)](https://github.com/griffithben "griffithben (1 commits)")[![shine-on](https://avatars.githubusercontent.com/u/1013956?v=4)](https://github.com/shine-on "shine-on (1 commits)")

---

Tags

databaseeloquentmodelsmysqlpeceepgsqlphpqueryquery-builderquerybuildersqlitedatabasemysqlsqlitepostgresqlsqleloquentquerybuilderquery builderpeceepixie

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/pecee-pixie/health.svg)

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

###  Alternatives

[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)[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)[delight-im/db

Safe and convenient SQL database access in a driver-agnostic way

49156.8k7](/packages/delight-im-db)

PHPackages © 2026

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