PHPackages                             drift/dbal - 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. drift/dbal

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

drift/dbal
==========

DBAL for ReactPHP on top of Doctrine

0.1.5(4y ago)4222.5k↓42.9%6[1 issues](https://github.com/driftphp/reactphp-dbal/issues)[1 PRs](https://github.com/driftphp/reactphp-dbal/pulls)1MITPHPPHP ^7.4 || ^8.0

Since Feb 25Pushed 2y ago5 watchersCompare

[ Source](https://github.com/driftphp/reactphp-dbal)[ Packagist](https://packagist.org/packages/drift/dbal)[ RSS](/packages/drift-dbal/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (7)Versions (8)Used By (1)

DBAL for ReactPHP
=================

[](#dbal-for-reactphp)

[![CircleCI](https://camo.githubusercontent.com/9b778d6c9de4fce21de799ea50bc51d94064e288fd41869dfc406589697a4692/68747470733a2f2f636972636c6563692e636f6d2f67682f64726966747068702f72656163747068702d6462616c2e7376673f7374796c653d737667)](https://circleci.com/gh/driftphp/reactphp-dbal)

This is a DBAL on top of ReactPHP SQL clients and Doctrine model implementation. You will be able to use

- Doctrine QueryBuilder model
- Doctrine Schema model
- Easy-to-use shortcuts for common operations
- and much more support is being added right now

> Attention. Only for proof of concept ATM. Do not use this library on production until the first stable version is tagged.

Example
-------

[](#example)

Let's create an example of what this library can really do. For this example, we will create an adapter for Mysql, and will use Doctrine QueryBuilder to create a new element in database and query for some rows.

Because we will use Mysql adapter, you should have installed the ReactPHP based mysql library `react/mysql`. Because this library is on development stage, all adapters dependencies will be loaded for testing purposes.

First of all, we need to create a Connection instance with the selected platform driver. We will have to create as well a Credentials instance with all the connection data.

```
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Drift\DBAL\Connection;
use Drift\DBAL\Driver\Mysql\MysqlDriver;
use Drift\DBAL\Credentials;
use React\EventLoop\Factory as LoopFactory;

$loop = LoopFactory::create();
$mysqlPlatform = new MySqlPlatform();
$mysqlDriver = new MysqlDriver($loop);
$credentials = new Credentials(
   '127.0.0.1',
   '3306',
   'root',
   'root',
   'test'
);

$connection = Connection::createConnected(
    $mysqlDriver,
    $credentials,
    $mysqlPlatform
);
```

Once we have the connection, we can create a new register in the database by using the Doctrine QueryBuilder or direct built-in methods. The result of all these calls will be a Promise interface that, eventually, will return a Result instance.

```
use Drift\DBAL\Connection;
use Drift\DBAL\Result;

/**
* @var Connection $connection
 */
$promise = $connection
    ->insert('test', [
        'id' => '1',
        'field1' => 'val1',
        'field2' => 'val2',
    ])
    ->then(function(Result $_) use ($connection) {
        $queryBuilder = $connection->createQueryBuilder();

        return $connection
            ->query($queryBuilder)
            ->select('*')
            ->from('test', 't')
            ->where($queryBuilder->expr()->orX(
                $queryBuilder->expr()->eq('t.id', '?'),
                $queryBuilder->expr()->eq('t.id', '?')
            ))
            ->setParameters(['1', '2']);
    })
    ->then(function(Result $result) {
        $numberOfRows = $result->fetchCount();
        $firstRow = $result->fetchFirstRow();
        $allRows = $result->fetchAllRows();
    });
```

You can use, at this moment, adapters for `mysql`, `postgresql`, and `sqlite`.

Connection shortcuts
--------------------

[](#connection-shortcuts)

This DBAL introduce some shortcuts useful for your projects on top of Doctrine query builder and escaping parametrization.

### Insert

[](#insert)

Inserts a new row in a table. Needs the table and an array with fields and their values. Returns a Promise.

```
$connection->insert('test', [
    'id' => '1',
    'field1' => 'value1'
]);
```

### Update

[](#update)

Updates an existing row from a table. Needs the table, an identifier as array and an array of fields with their values. Returns a Promise.

```
$connection->update(
    'test',
    ['id' => '1'],
    [
        'field1' => 'value1',
        'field2' => 'value2',
    ]
);
```

### Upsert

[](#upsert)

Insert a row if not exists. Otherwise, it will update the existing row with given values. Needs the table, an identifier as array and an array of fields with their values. Returns a Promise.

```
$connection->upsert(
    'test',
    ['id' => '1'],
    [
        'field1' => 'value1',
        'field2' => 'value2',
    ]
);
```

### Delete

[](#delete)

Deletes a row if exists. Needs the table and the identifier as array. Returns a Promise.

```
$connection->delete('test', [
    'id' => '1'
]);
```

### Find one by

[](#find-one-by)

Find a row given a where clause. Needs the table and an array of fields with their values. Returns a Promise with, eventually, the result as array of all found rows.

```
$connection
    ->findOneById('test', [
        'id' => '1'
    ])
    ->then(function(?array $result) {
        if (is_null($result)) {
            // Row with ID=1 not found
        } else {
            // Row with ID=1 found.
            echo $result['id'];
        }
    });
```

### Find by

[](#find-by)

Find all rows given an array of where clauses. Needs the table and an array of fields with their values. Returns a Promise with, eventually, the result as array of all found rows.

```
$connection
    ->findBy('test', [
        'age' => '33'
    ])
    ->then(function(array $result) {
        echo 'Found ' . count($result) . ' rows';
    });
```

### Create table

[](#create-table)

You can easily create a new table with basic information. Needs the table name and an array of fields and types. Strings are considered with length `255`. First field position in the array will be considered as primary key. Returns a Promise with, eventually, the connection.

```
$connection->createTable('test', [
    'id' => 'string',
    'name' => 'string',
]);
```

This is a basic table creation method. To create more complex tables, you can use Doctrine's Schema model. You can execute all Schema SQLs generated by using this method inside Connection named `executeSchema`. You'll find more information about this Schema model in [Doctrine documentation](https://www.doctrine-project.org/projects/doctrine-dbal/en/2.10/reference/schema-representation.html)

```
$schema = new Schema();
$table = $schema->createTable('test');
$table->addColumn('id', 'string');
// ...

$connection->executeSchema($schema);
```

### Drop table

[](#drop-table)

You can easily drop an existing table. Needs just the table name, and returns, eventually, the connection.

```
$connection->dropTable('test');
```

### Truncate table

[](#truncate-table)

You can easily truncate an existing table. Needs just the table name, and returns, eventually, the connection.

```
$connection->truncateTable('test');
```

Tests
-----

[](#tests)

You can run tests by running `docker-compose up` and by doing `php vendor/bin/phpunit`.

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity38

Limited adoption so far

Community21

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~155 days

Recently: every ~194 days

Total

6

Last Release

1495d ago

PHP version history (2 changes)0.1.0PHP ^7.3

0.1.3PHP ^7.4 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/e8cb88fa6413489c9cdb77288a06ff94b4553aa497d8fc0ee1891576053f1306?d=identicon)[mmoreram](/maintainers/mmoreram)

---

Top Contributors

[![mmoreram](https://avatars.githubusercontent.com/u/521409?v=4)](https://github.com/mmoreram "mmoreram (23 commits)")[![bartvanhoutte](https://avatars.githubusercontent.com/u/4867154?v=4)](https://github.com/bartvanhoutte "bartvanhoutte (11 commits)")[![marinhekman](https://avatars.githubusercontent.com/u/89514754?v=4)](https://github.com/marinhekman "marinhekman (7 commits)")[![seregazhuk](https://avatars.githubusercontent.com/u/9959761?v=4)](https://github.com/seregazhuk "seregazhuk (5 commits)")[![Lloople](https://avatars.githubusercontent.com/u/5665466?v=4)](https://github.com/Lloople "Lloople (1 commits)")[![developernaren](https://avatars.githubusercontent.com/u/3628468?v=4)](https://github.com/developernaren "developernaren (1 commits)")[![enumag](https://avatars.githubusercontent.com/u/539462?v=4)](https://github.com/enumag "enumag (1 commits)")

---

Tags

dbaldoctrinemysqlpostgresqlreactphpsqlite

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/drift-dbal/health.svg)

```
[![Health](https://phpackages.com/badges/drift-dbal/health.svg)](https://phpackages.com/packages/drift-dbal)
```

###  Alternatives

[scienta/doctrine-json-functions

A set of extensions to Doctrine that add support for json query functions.

58723.9M36](/packages/scienta-doctrine-json-functions)[martin-georgiev/postgresql-for-doctrine

Extends Doctrine with native PostgreSQL support for arrays, JSONB, ranges, PostGIS geometries, text search, ltree, uuid, and 100+ PostgreSQL-specific functions.

4485.3M4](/packages/martin-georgiev-postgresql-for-doctrine)[damienharper/auditor-bundle

Integrate auditor library in your Symfony projects.

4542.8M](/packages/damienharper-auditor-bundle)[sonata-project/entity-audit-bundle

Audit for Doctrine Entities

644989.8k1](/packages/sonata-project-entity-audit-bundle)[overtrue/laravel-versionable

Make Laravel model versionable.

585308.0k5](/packages/overtrue-laravel-versionable)[worksome/foggy

Foggy is a tool for making database dumps with some data removed/changed.

26571.7k1](/packages/worksome-foggy)

PHPackages © 2026

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