PHPixie Database library

3.10.1 2018-02-16 00:32 UTC


Supports a common query interface for MySQL, PostgreSQL, SQLite and MongoDB

Build Status Test Coverage Code Climate HHVM Status

Author Source Code Software License Total Downloads


$slice = new \PHPixie\Slice();
$database = new \PHPixie\Database($slice->arrayData(array(
    'default' => array(
        'driver' => 'pdo',
        'connection' => 'sqlite::memory:'

If you are using the PHPixie Framework the Database component is automatically initalized for you. Access it via $frameworkBuilder->components()->database() and configure in the /config/database.php file.

return array(
    //You can define multiple connections
    //each with a different name
    'default' => array(
        'driver'     => 'pdo',
        'connection' => 'mysql:host=localhost;dbname=phpixie',
        //or SQLite
        'connection' => 'sqlite:/some/file',
        //or Postgres
        'connection' => 'pgsql:dbname=exampledb',
        'user'       => 'root',
        'password'   => 'password'
    'other' => array(
        //connecting to MongoDD
        'driver'   => 'mongo',
        'database' => 'phpixie',
        'user'     => 'pixie',
        'password' => 'password'


Querying relational database and MongoDB collections is very similar in PHPixie. Let's look at relational databases first

$connection = $database->get('default');

// SELECT * FROM `posts` WHERE `status`='published'
$query = $connection->selectQuery();
$posts = $query
    ->where('status', 'Published')
// Specifying fields

// You can remove limit, offset
// specified fields, etc from the query
// using clearSomething()

//And get it using getSomething()

//Using OR and XOR logic
    ->where('status', 'published')
    ->orWhereNot('status', 'deleted')
    ->xorWhere('id', 5);
//Shorthand functions
    ->and('status', 'published')
    ->orNot('status', 'deleted')
    ->xor('id', 5);

// WHERE `status` = 'published'
// OR NOT (`id` = 4 AND `views` = 5)
    ->where('status', 'published')
        ->where('id', 4)
        ->and('views', 4)

// Less verbose syntax
    ->where('status', 'published')
    ->or(function(query) {
            ->where('id', 4)
            ->and('views', 4);
// More verbose syntax
// Useful for programmatic filters
        $logic    = 'and',
        $negate   = false,
        $field    = 'status',
        $operator = '=',
        $logic    = 'and',
        $negate   = false

Using and, or and xor add conditions to the last used conditon type. So calling or after where() will be same as orWhere(), while using it after having() will be considered as orHaving().


// So far we only compared fields with values
// But there are other operators available

// >, < , >=, <=, '!='
$query->where('views', '>', 5);

// comparies fields to other fields
// can be done by adding an '*'
$query->where('votes', '>=*', 'votesRequired');

// Between
$query->where('votes', 'between', 5, 6);

// In
$query->where('votes', 'in', array(5, 6));

// Like
$query->where('name', 'like', 'Welcome%');

// Regexp
$query->where('name', 'regexp', '.*');

// SQL expression
$expression = $database->sqlExpression('LOWER(?)', array('text'));
$query->where('title', $expression);

// You can also use it for fields
// SELECT COUNT(1) as `count`
$expression = $database->sqlExpression('COUNT(1)');
    'count' => $expression

Tables, Subqueries and JOINs

// When specofying a table
// you can also define an alias for it
$query->table('posts', 'p');

// INNER JOIN `categories`

// LEFT JOIN `categories` AS `c`
$query->join('categories', 'c', 'left')

    ->on('p.categoryId', 'c.categoryId');
// The on() conditions can be used in
// the same way as where(), and apply
// to the last join() statement
    ->join('categories', 'c', 'left')
        ->on('p.categoryId', '')
        ->or('p.parentCategoryId', '')
        ->on('p.authorId', '');
// You can use subqueries as tables,
// but you must supply the alias parameter

$query->join($subqeury, 'c', 'left')

$query->union($subquery, $all = true);


After you define you fields you cn use HAVING in the same way you would use WHERE;

        'count' => $database->sqlExpression('COUNT(1)');
    ->having('count', '>', 5)
    ->or('count', '<', 2);

Other types of queries

// Delete syntax is very similar to select
// except it doesn't support HAVING syntax
    ->where('id', 5)

// Count query is a shorthand that returns the count
// of matched items
$count = $connection->countQuery()
    ->where('id', '>', 5)

// Inserting
$insertQuery = $connection->insertQuery();
    'id'    => 1,
    'title' => 'Hello'

// Insert multiple rows
    array('id', 'title'),
        array(1, 'Hello'),
        array(2, 'World'),

// Getting insert id

// Updating
$updateQuery = $connection->updateQuery();
    ->set('name', 'Hello')
    ->where('id', 4)

// increment values
        'views' => 1


Query placeholders are another way to ease programmatic query building. You can create a placeholder and then later replace it with actual conditions. Here is an example:

    ->where('status', 'published')

// Add placeholder inside the OR goup
$placeholder = $query->addPlaceholder(
    $logic  = 'and',
    $negate = false,
    $allowEmpty = false

        ->and('views', '>', 5);

// so far this results in
// WHERE `status` = 'published'
// OR (<placeholder> AND `views` > 5)

// Now we can replace the placeholder by
// adding conditions to it
$placeholder->where('votes', '>', 5);


The basic usage for transactions is to rollback them if an exception occured and then rethrow the exception

// ...
try {
    // ...
} catch(\Exception $e) {
    throw $e;

PHPixie also supports transaction savepoints which can be used to for some more adavcenced behavior:

$name = $database->savepointTransaction();


Querying MongoDB is very similar to querying SQL databases. Of course you can not use relational methods like JOIN and HAVING statements, transactions, etc. But instead you get additional features in addition:

$posts = $query
    // subdocument conditions
    ->where('', 'Dracony')

    ->set('done', true)
    ->unset(array('started', 'inProgress'))
            'name' => 'Trixie'
            'name' => 'Stella'

An easier way of querying subdocuments can be achieved using subdocument groups:

//setting conditions for subdocuments
        ->where('name', 'Dracony')
//setting conditions for subarray items
        ->where('name', 'Dracony')