PHPackages                             anax/database-query-builder - 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. anax/database-query-builder

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

anax/database-query-builder
===========================

Anax Database Query Builder module, create queries through methods.

v2.0.4(6y ago)16.3k↓91.7%2MITPHPPHP &gt;=7.2CI failing

Since Mar 31Pushed 6y ago1 watchersCompare

[ Source](https://github.com/canax/database-query-builder)[ Packagist](https://packagist.org/packages/anax/database-query-builder)[ Docs](https://dbwebb.se/anax)[ RSS](/packages/anax-database-query-builder/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependencies (2)Versions (27)Used By (2)

Anax Database Query Builder
===========================

[](#anax-database-query-builder)

[![Latest Stable Version](https://camo.githubusercontent.com/c392370ef2486e617907f10a955632352120d95949f4ef98095ced9003293189/68747470733a2f2f706f7365722e707567782e6f72672f616e61782f64617461626173652d71756572792d6275696c6465722f762f737461626c65)](https://packagist.org/packages/anax/database-query-builder)[![Join the chat at https://gitter.im/canax/database-query-builder](https://camo.githubusercontent.com/9db7869a5438d505a207225dd5f07d476fc73223a3f9e8d7495bda23e485a715/68747470733a2f2f6261646765732e6769747465722e696d2f63616e61782f64617461626173652d71756572792d6275696c6465722e737667)](https://gitter.im/canax/database-query-builder?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

[![Build Status](https://camo.githubusercontent.com/c29dac7a38edcc414339b2a375e32f8cfe17a509db6d082dfbc25679265e41d8/68747470733a2f2f7472617669732d63692e6f72672f63616e61782f64617461626173652d71756572792d6275696c6465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/canax/database-query-builder)[![CircleCI](https://camo.githubusercontent.com/ebd2cf48a81bd171d15165f755ed0b8920037b3c48867d4d273f9c8d99cc87b1/68747470733a2f2f636972636c6563692e636f6d2f67682f63616e61782f64617461626173652d71756572792d6275696c6465722e7376673f7374796c653d737667)](https://circleci.com/gh/canax/database-query-builder)

[![Build Status](https://camo.githubusercontent.com/4f001cd63ac36ab25dcba1ea4ccb714820a4d25dac7eec0167a283e863fcfd20/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f63616e61782f64617461626173652d71756572792d6275696c6465722f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/canax/database-query-builder/build-status/master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/e52af1184a3b2f35ead118e78ae4191f13bc59b79024da966df1b7d3eabf8a3b/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f63616e61782f64617461626173652d71756572792d6275696c6465722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/canax/database-query-builder/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/332638214845953a541bc9e9c2236ee5fb9175e7e21c4d5ddefff6c3db94e00d/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f63616e61782f64617461626173652d71756572792d6275696c6465722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/canax/database-query-builder/?branch=master)

[![Maintainability](https://camo.githubusercontent.com/946792c3b282bfa243a3c0f47000b95d8fc9215667cc20de52be464a4bc936de/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f61623063346434373235363564393565363466662f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/canax/database-query-builder/maintainability)[![Codacy Badge](https://camo.githubusercontent.com/10cc4009e476d29c979750f06bdeb0f0fb22bb812a0de7dfc9eafc9a43dab7e3/68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f3664666636303434643235363436653962636165613361333333313038646564)](https://www.codacy.com/app/mosbth/database-query-builder?utm_source=github.com&utm_medium=referral&utm_content=canax/database-query-builder&utm_campaign=Badge_Grade)

Anax Database Query Builder module as an extension to [`anax/database`](https://github.com/canax/database) to enable querying the datase using methods instead of SQL.

This module is used to implement the module Database Active Record [`anax\database-active-record`](https://github.com/canax/database-active-record).

The module is tested using MySQL and SQLite.

Table of content
----------------

[](#table-of-content)

- [Install](#Install)
- [Development](#Development)
- [Class, interface, trait](#class-interface-trait)
- [Exceptions](#exceptions)
- [DI service](#di-service)
- [Access as framework service](#access-as-framework-service)
- [Basic usage](#Basic-usage)
- [Dependency](#Dependency)
- [License](#License)

Install
-------

[](#install)

You can install the module from [`anax/database-query-builder` on Packagist](https://packagist.org/packages/anax/database-query-builder) using composer.

```
composer require anax/database-query-builder

```

You can then copy the default configuration files as a start.

```
# In the root of your Anax installation
rsync -av vendor/anax/database-query-builder/config .

```

Development
-----------

[](#development)

To work as a developer you clone the repo and install the local environment through make. Then you can run the unit tests.

```
make install
make test

```

Class, interface, trait
-----------------------

[](#class-interface-trait)

The following classes, interfaces and traits exists.

The following parts are related to the feature of a SQL query builder.

Class, interface, traitDescription`Anax\Database\QueryBuilderTrait`A trait implementing SQL query builder, based upon `Anax\Database\Database`.`Anax\Database\DatabaseQueryBuilder`A database class using the SQL query builder trait (used by the Active Record module) and extending the database class.Exceptions
----------

[](#exceptions)

All exceptions are in the namespace `Anax\DatabaseQueryBuilder\Exception\`. The following exceptions exists and may be thrown.

ExceptionDescription`BuildException`When failing to build a SQL query.DI service
----------

[](#di-service)

The database query builder is created as a framework service within `$di`. The following is a sample on how the database query builder service is created through `config/di/dbqb.php`.

```
/**
 * Configuration file for database query builder service.
 */
return [
    // Services to add to the container.
    "services" => [
        "dbqb" => [
            "shared" => true,
            "callback" => function () {
                $obj = new \Anax\DatabaseQueryBuilder\DatabaseQueryBuilder();

                // Load the configuration files
                $cfg = $this->get("configuration");
                $config = $cfg->load("database");

                // Set the database configuration
                $connection = $config["config"] ?? [];
                $db->setOptions($connection);
                $db->setDefaultsFromConfiguration();

                return $db;
            }
        ],
    ],
];
```

Access as framework service
---------------------------

[](#access-as-framework-service)

You can access the module as a framework service and use it as an ordinary database service.

```
$sql = "SELECT * FROM movie;";

$db = $di->get("dbqb");
$db->connect();
$res = $db->executeFetchAll($sql);
```

This is since the class `\Anax\DatabaseQueryBuilder\DatabaseQueryBuilder` extends the database class `\Anax\Database\Database`.

Basic usage
-----------

[](#basic-usage)

This is the basic usage of the query builder.

You start by creating a database object from the query builder class and connect to the database.

```
$this->db = new DatabaseQueryBuilder([
    "dsn" => "sqlite::memory:",
]);
$this->db->setDefaultsFromConfiguration();
$this->db->connect();
```

This is more or less the same as retrieving the class from the $di container.

You can now create a table.

```
// Create a table
$this->db->createTable(
    'user',
    [
        'id'    => ['integer', 'primary key', 'not null'],
        'age'   => ['integer'],
        'name'  => ['varchar(10)']
    ]
)->execute();
```

The table is created.

You can now insert rows into the table.

```
$this->db->insert(
    "user",
    [
        "age" => 3,
        "name" => "three",
    ]
)->execute();

$last = $this->db->lastInsertId(); // 1
$rows = $this->db->rowCount();     // 1
```

You can now query the table.

```
$res = $this->db->select("*")
                ->from("user")
                ->where("id = 1")
                ->execute()
                ->fetch();

$res->id;   // 1
$res->age;  // 3
$res->name; // "three"
```

That is the basic usage and the idea is to create the SQL-queries using class methods and build tha actual SQL query behind the scene.

Dependency
----------

[](#dependency)

This module depends upon, and extends, the database abstraction layer [`anax\database`](https://github.com/canax/database).

The module is usually used within an Anax installation but can also be used without Anax.

License
-------

[](#license)

This software carries a MIT license. See [LICENSE.txt](LICENSE.txt) for details.

```
 .
..:  Copyright (c) 2013 - 2018 Mikael Roos, mos@dbwebb.se

```

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 97.1% 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 ~39 days

Recently: every ~92 days

Total

26

Last Release

2387d ago

Major Versions

v1.1.10 → v2.0.0-alpha.12018-10-01

PHP version history (2 changes)v1.0.2PHP &gt;=5.6

v2.0.0-beta.1PHP &gt;=7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/4cac244120f54c02a4fb3186b91323f3f0805dd9daefd0babeb4a82a19232faf?d=identicon)[mikael\_roos](/maintainers/mikael_roos)

---

Top Contributors

[![mosbth](https://avatars.githubusercontent.com/u/169550?v=4)](https://github.com/mosbth "mosbth (67 commits)")[![NiklasAurora](https://avatars.githubusercontent.com/u/11875601?v=4)](https://github.com/NiklasAurora "NiklasAurora (2 commits)")

---

Tags

databasequery builderdba

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/anax-database-query-builder/health.svg)

```
[![Health](https://phpackages.com/badges/anax-database-query-builder/health.svg)](https://phpackages.com/packages/anax-database-query-builder)
```

###  Alternatives

[usmanhalalit/pixie

A lightweight, expressive, framework agnostic query builder for PHP.

6872.2M16](/packages/usmanhalalit-pixie)[foolz/sphinxql-query-builder

A PHP query builder for SphinxQL and ManticoreQL with MySQLi and PDO drivers.

3232.2M33](/packages/foolz-sphinxql-query-builder)[ezsql/ezsql

Advance database access library. Make interacting with a database ridiculously easy. An universal interchangeable CRUD system.

86848.3k](/packages/ezsql-ezsql)[lulco/phoenix

Database Migrations for PHP

179341.0k4](/packages/lulco-phoenix)[jv2222/ezsql

Advance database access library. Make interacting with a database ridiculously easy. An universal interchangeable CRUD system.

86811.4k2](/packages/jv2222-ezsql)[cycle/database

DBAL, schema introspection, migration and pagination

65746.2k48](/packages/cycle-database)

PHPackages © 2026

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