PHPackages                             harp-orm/query - 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. harp-orm/query

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

harp-orm/query
==============

SQL Builder for PDO

0.2.3(12y ago)1195621BSD-3-ClausePHPPHP &gt;=5.3.0

Since Dec 21Pushed 11y ago7 watchersCompare

[ Source](https://github.com/harp-orm/query)[ Packagist](https://packagist.org/packages/harp-orm/query)[ RSS](/packages/harp-orm-query/feed)WikiDiscussions master Synced today

READMEChangelog (5)Dependencies (2)Versions (6)Used By (1)

Harp Query
==========

[](#harp-query)

[![Build Status](https://camo.githubusercontent.com/ecf252a5478c138c9e3fa437cf6ac483c086b0ce9482347e7c7d8cd3c94adb19/68747470733a2f2f7472617669732d63692e6f72672f686172702d6f726d2f71756572792e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/harp-orm/query)[![Scrutinizer Quality Score](https://camo.githubusercontent.com/8155b05ff54a5e498ccaea920cf1f951d761db056e9654f3fcb12975780ef4e0/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f686172702d6f726d2f71756572792f6261646765732f7175616c6974792d73636f72652e706e673f733d34323938383063323536363361346330633437363866626234313538616265303438373236653832)](https://scrutinizer-ci.com/g/harp-orm/query/)[![Code Coverage](https://camo.githubusercontent.com/5bdc4145a929172730e815235ab9d116a8e7101104fdcf4bf1c3b333f229c181/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f686172702d6f726d2f71756572792f6261646765732f636f7665726167652e706e673f733d65333230383863363832653637643163376565633238623538663963366133346132313233656437)](https://scrutinizer-ci.com/g/harp-orm/query/)[![Latest Stable Version](https://camo.githubusercontent.com/d947080b4b798621df7f67ecbbb040bb55db535bf36ec6936ef08604b7a10f73/68747470733a2f2f706f7365722e707567782e6f72672f686172702d6f726d2f71756572792f762f737461626c652e737667)](https://packagist.org/packages/harp-orm/query)

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

[](#quick-usage-example)

```
use Harp\Query\DB;

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

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

foreach ($query->execute() as $row) {
    var_dump($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.

Why?
----

[](#why)

PHP has quite a lot of excellent query builder classes already. For example [Paris/Idiom](http://j4mie.github.io/idiormandparis/), [Kohana Query Builder](http://kohanaframework.org/3.3/guide/database/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](http://us3.php.net/manual/en/book.pdo.php)
- 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)

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(
    'mysql:dbname=test-db;host=127.0.0.1',
    'root',
    'mypass',
    [PDO::ATTR\_DEFAULT\_FETCH\_MODE => PDO::FETCH\_BOTH]
);
$db->getPdo();
```

You can set some additional option for the DB object

```
$logger = new NullLogger(); // Some PSR logger
$db->setLogger($logger);

// Standard sql '"' double quotes to escape table / column names
$db->setEscaping(DB::ESCAPING_STANDARD);

// Mysql '`' backticks to escape table / column names
// This is the default option
$db->setEscaping(DB::ESCAPING_MYSQL);

// No escaping for table / column names
$db->setEscaping(DB::ESCAPING_NONE);

```

Retrieving data (Select)
------------------------

[](#retrieving-data-select)

Tretrieve data from the database, use [Select class](/src/Select.php).

An example select:

```
use Harp\Query\DB;

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

$select = $db->select()
    ->from('users')
    ->column('users.*')
    ->where('username', 'Tom')
    ->whereIn('type', ['big', 'small'])
    ->limit(10)
    ->order('created_at', 'DESC');

$result = $select->execute();

foreach ($result as $row) {
    var_dump($row);
}
```

Inserting data (Insert)
-----------------------

[](#inserting-data-insert)

Tretrieve insert new data to the database, use [Insert class](/src/Insert.php).

An example insert:

```
use Harp\Query\DB;

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

$insert = $db->insert()
    ->into('users')
    ->set([
        'name' => 'Tom',
        'family_name' => 'Soyer'
    ]);

$insert->execute();

echo $insert->getLastInsertId();
```

Deleting data (Delete)
----------------------

[](#deleting-data-delete)

Tretrieve delete data from the database, use [Delete class](/src/Delete.php).

An example delete:

```
use Harp\Query\DB;

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

$delete = $db->delete()
    ->from('users')
    ->where('score', 10)
    ->limit(10);

$delete->execute();
```

Updating data (Update)
----------------------

[](#updating-data-update)

Tretrieve update data in the database, use [Update class](/src/Update.php).

An example update:

```
use Harp\Query\DB;

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

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

$update->execute();
```

Detailed docs
-------------

[](#detailed-docs)

- [Select](/docs/Select.md)
- [Insert](/docs/Insert.md)
- [Delete](/docs/Delete.md)
- [Update](/docs/Update.md)
- [Union](/docs/Union.md)

License
-------

[](#license)

Copyright (c) 2014, Clippings Ltd. Developed by Ivan Kerin as part of [clippings.com](http://clippings.com)

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

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 83.2% 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 ~49 days

Total

5

Last Release

4380d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/506129?v=4)[Harry Dobrev](/maintainers/hkdobrev)[@hkdobrev](https://github.com/hkdobrev)

---

Top Contributors

[![ivank](https://avatars.githubusercontent.com/u/4976?v=4)](https://github.com/ivank "ivank (134 commits)")[![hkdobrev](https://avatars.githubusercontent.com/u/506129?v=4)](https://github.com/hkdobrev "hkdobrev (27 commits)")

### Embed Badge

![Health badge](/badges/harp-orm-query/health.svg)

```
[![Health](https://phpackages.com/badges/harp-orm-query/health.svg)](https://phpackages.com/packages/harp-orm-query)
```

###  Alternatives

[symfony/cache

Provides extended PSR-6, PSR-16 (and tags) implementations

4.2k373.5M3.3k](/packages/symfony-cache)[matomo/matomo

Matomo is the leading Free/Libre open analytics platform

21.7k38.9k](/packages/matomo-matomo)[kimai/kimai

Kimai - Time Tracking

4.8k9.0k1](/packages/kimai-kimai)[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k15](/packages/tempest-framework)[cycle/database

DBAL, schema introspection, migration and pagination

71777.8k53](/packages/cycle-database)[api-platform/metadata

API Resource-oriented metadata attributes and factories

275.0M219](/packages/api-platform-metadata)

PHPackages © 2026

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