0.2.3 2014-07-07 11:52 UTC


Build Status Scrutinizer Quality Score Code Coverage Latest Stable Version

A query builder library, extending PDO to allow writing queries in object oriented style. Intelligently manages passing parameters to PDO's execute.

Quick usage example

use Harp\Query\DB;

$db = new DB('mysql:dbname=test-db;host=', 'root');

$query = $db->select()
    ->where('seller', true)
    ->join('profiles', ['profiles.user_id' => 'users.id'])

foreach ($query->execute() as $row) {

echo "Executed:\n";
echo $query->humanize();

The query execute() method will generate a PDOStatement object that can be iterated over. All the variables are passed as position parameters ("seller = ?") so are properly escaped by the PDO driver.


PHP has quite a lot of excellent query builder classes already. For example Paris/Idiom, Kohana Query Builder etc. Why have another one? Here is my elevator pitch:

  • Integrate with PDO - since it already has quite a lot of support for different DB drivers, harp-orm/query can use all that wealth of functionality out of the box. The base DB class extends PDO class, and the select result is actually PDOStatement object. And all of this already has great docs
  • Use PSR coding standards and Symfony naming conventions for more familiar and readable codebase.
  • Use PSR logging to integrate with any compatible logger.
  • Support more rarely used SQL constructs, e.g. INSERT IGNORE, UNION, UPDATE JOIN etc.
  • Precise methods to express intent more clearly e.g. methods like where, whereIn, whereLike allow you to write exactly what you intend, and not expect guesswork from the library. This can provide more error-free codebase.
  • Fully covered with DockBlocks so static code analysis on packages built on top of it can be more accurate
  • Full test coverage

Connecting to the database

Connecting to the database is a done with the "DB" object. It lazy loads a PDO connection object, and has the same arguments.

use Harp\Query\DB;

$db = new DB(

You can set some additional option for the DB object

$logger = new NullLogger(); // Some PSR logger

// Standard sql '"' double quotes to escape table / column names

// Mysql '`' backticks to escape table / column names
// This is the default option

// No escaping for table / column names

Retrieving data (Select)

Tretrieve data from the database, use Select class.

An example select:

use Harp\Query\DB;

$db = new DB('mysql:dbname=test-db;host=', 'root');

$select = $db->select()
    ->where('username', 'Tom')
    ->whereIn('type', ['big', 'small'])
    ->order('created_at', 'DESC');

$result = $select->execute();

foreach ($result as $row) {

Inserting data (Insert)

Tretrieve insert new data to the database, use Insert class.

An example insert:

use Harp\Query\DB;

$db = new DB('mysql:dbname=test-db;host=', 'root');

$insert = $db->insert()
        'name' => 'Tom',
        'family_name' => 'Soyer'


echo $insert->getLastInsertId();

Deleting data (Delete)

Tretrieve delete data from the database, use Delete class.

An example delete:

use Harp\Query\DB;

$db = new DB('mysql:dbname=test-db;host=', 'root');

$delete = $db->delete()
    ->where('score', 10)


Updating data (Update)

Tretrieve update data in the database, use Update class.

An example update:

use Harp\Query\DB;

$db = new DB('mysql:dbname=test-db;host=', 'root');

$update = $db->update()
    ->set(['name' => 'New Name'])
    ->where('id', 10);


Detailed docs


Copyright (c) 2014, Clippings Ltd. Developed by Ivan Kerin as part of clippings.com

Under BSD-3-Clause license, read LICENSE file.