Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

pixie

> 编程语言
Open source

Database query builder for PHP, framework agnostic, lightweight and expressive.

676 stars0 likes1 views
WebsiteGitHub

About

Database query builder for PHP, framework agnostic, lightweight and expressive.

This project is Not Actively Maintained but most of the features are fully working and there are no major security issues, I'm just not giving it much time.

Pixie Query Builder

A lightweight, expressive, framework agnostic query builder for PHP it can also be referred as a Database Abstraction Layer. Pixie 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

…

Simple Query:

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

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

Full Queries:

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

php
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

Pixie uses Composer to make things easy.

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

"usmanhalalit/pixie": "2.*@dev"

And run:

composer update

Library on Packagist.

Full Usage API

Table of Contents

  • Connection
    • Alias
    • Multiple Connection
    • SQLite and PostgreSQL Config Sample
  • Query
  • Select
    • Get Easily
    • Multiple Selects
    • Select Distinct
    • Get All
    • Get First Row
    • Get Rows Count
  • Where
    • Where In
    • Where Between
    • Where Null
    • Grouped Where
  • Group By and Order By
  • Having
  • Limit and Offset
  • Join
    • Multiple Join Criteria
  • Raw Query
    • Raw Expressions
  • Insert
    • Batch Insert
    • Insert with ON DUPLICATE KEY statement
  • Update
  • Delete
  • Transactions
  • Get Built Query
  • Sub Queries and Nested Queries
  • Get PDO Instance
  • Fetch results as objects of specified class
  • Query Events
    • Available Events
    • Registering Events
    • Removing Events
    • Some Use Cases
    • Notes

Connection

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.;

…

Alias

When you create a connection:

php
new \Pixie\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.

php
$connection = new \Pixie\Connection('mysql', $config);
$qb = new \Pixie\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

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

Query

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

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

Get Easily

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

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

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

Select

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

Multiple Selects

php
->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

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

Get All

Return an array.

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

You can loop through it like:

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

Get First Row

php
$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

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

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.

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

Where In

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

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

Where Between

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

Where Null

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

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.

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

Group By and Order By

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

Multiple Group By

php
->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

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

Limit and Offset

php
->limit(30);

->offset(10);

Join

php
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.

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

Multiple Join Criteria

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

php
->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

You can always use raw queries if you need,

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

var_dump($query->get());

You can also pass your bindings

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

Raw Expressions

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

php
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

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

insert() method returns the insert id.

Batch Insert

php
$data = array(
    array(
        'name'        => 'Sana',
        'description' => 'Blah'
    ),
    array(
        'name'        => 'Usman',
        '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

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

Update

php
$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

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

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

Transactions

Pixie has the ability to run database "transactions", in which all database changes are not saved unti

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHP

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言