PHPackages                             vivace/db - 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. vivace/db

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

vivace/db
=========

ORM

501[2 issues](https://github.com/php-vivace/db/issues)PHP

Since May 16Pushed 8y ago1 watchersCompare

[ Source](https://github.com/php-vivace/db)[ Packagist](https://packagist.org/packages/vivace/db)[ RSS](/packages/vivace-db/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (1)Used By (0)

vivace\\db
==========

[](#vivacedb)

[![Latest Stable Version](https://camo.githubusercontent.com/21e0b80affec3d5420694bca96876de7a96d42ff8ad1198c56b4f333c92c2618/68747470733a2f2f706f7365722e707567782e6f72672f7669766163652f64622f762f737461626c65)](https://packagist.org/packages/vivace/db)[![Total Downloads](https://camo.githubusercontent.com/a7e292c6958d43cf07436a7f17b6d159b45f757b975a192933047552a6b3685e/68747470733a2f2f706f7365722e707567782e6f72672f7669766163652f64622f646f776e6c6f616473)](https://packagist.org/packages/vivace/db)[![License](https://camo.githubusercontent.com/277a7400088eb7d6752a5e5146c040165089badc985d038be46a2e6d7193719a/68747470733a2f2f706f7365722e707567782e6f72672f7669766163652f64622f6c6963656e7365)](https://packagist.org/packages/vivace/db)[![composer.lock](https://camo.githubusercontent.com/7185b0c21baeb08297ee8183a8206d1e6847762860ba4eceb5407565b861cd63/68747470733a2f2f706f7365722e707567782e6f72672f7669766163652f64622f636f6d706f7365726c6f636b)](https://packagist.org/packages/vivace/db)[![Maintainability](https://camo.githubusercontent.com/dde2cc4678b6c2e343aee3e3466436d3d11226e8248be08e47899eb41e46a543/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f39393662393331383333326662323566353865302f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/php-vivace/db/maintainability)

Goals
-----

[](#goals)

Create a flexible query builder with relationship support and CRUD operations.

Requirements
------------

[](#requirements)

- php **&gt;= 7.1**

Supported databases
-------------------

[](#supported-databases)

- PostgreSQL **&gt;=9.5**
- MySQL **&gt;= 5.7**
- MongoDB (planned)

Installing
----------

[](#installing)

```
composer require vivace/db

```

Usage
-----

[](#usage)

Initialize driver for your database. In this example, the driver for postgresql.

```
$pdo = new \PDO('dsn', 'user', 'pass');
$driver = new \vivace\db\sql\PostrgeSQL\Driver($pdo);
```

Initialize storage objects.

```
$userStorage = new \vivace\db\sql\Storage($driver, 'users');
```

Now you can use created storages for data manipulation.

Save the data to your storage.

```
$ok = $userStorage->save(['name' => 'Zoe Saldana', 'career' => 'actor', 'rating' => 4.95]);
```

Let's try fetch saved data from storage.

```
$user = $userStorage->filter(['name' => 'Zoe Saldana'])->fetch()->one();
// $user is simple assoc array.
var_dump($user);
```

Now it's time to change the data

```
$user['age'] = 39;
```

And save changes in storage.

```
$ok = $userStorage->save($user);
```

More examples.
--------------

[](#more-examples)

#### Filtering.

[](#filtering)

```
$users = $userStorage
    ->limit(100)
    // equalent SQL condition "career IN ('actor', 'producer') OR age >= 40"
    ->filter(['or', ['in', 'career', ['actor', 'producer'], ['>=', 'age', 40]])
    ->fetch();
```

#### Insert/Update

[](#insertupdate)

Insert one row

```
$ok = $userStorage->save(['name' => 'Mark Rufallo']);
```

Multiple rows

```
$ok = $userStorage->save([
    ['name' => 'Mark Rufallo'],
    ['name' => 'Chris Evans', 'rating' => 4.95],
]);
```

Update if exists, otherwise insert

```
$ok = $userStorage->save([
    ['id' => 6, 'name' => 'Mark Ruffalo'],// This data will be updated, because 'id' is the primary key
    ['name' => 'Chris Hemsworth'] // This data will be inserted as new row
]);
```

#### Update by condition.

[](#update-by-condition)

```
$numberOfupdated = $userStorage
    ->sort(['id' => -1])// Sorting by `id` in descending order
    ->skip(100)// skip first 100 found rows
    ->update(['career' => 'actor']);
```

#### Delete by condition.

[](#delete-by-condition)

```
$numberOfDeleted = $userStorage
    ->filter(['age' => 18, 'rating' => 5])
    ->and(['in', 'career', ['actor', 'producer'])
    // Delete all users by condition "age = 18 AND rating = 5 AND career IN ('actor', 'producer')"
    ->delete();
```

#### Relations.

[](#relations)

```
$userStorage = $userStorage->projection([
    // OneToOne
    'country' => new \vivace\Relation\Single($countryStorage, ['country_id' => 'id']),
    // OneToMany
    'rewards' => new \vivace\Relation\Many($rewardsStorage, ['id' => 'user_id'])
]);

$users = $userStorage->fetch()->all();

foreach($users as $user){
    if(isset($user['country'])) {
        var_dump($user['country']);
    }
    foreach($user['rewards'] as $reward){
        var_dump($reward);
    }
}
```

#### Field aliases.

[](#field-aliases)

```
$userStorage = $userStorage->projection([
    'rank' => 'rating'
]);

// Aliases are available for use in the condition.
$user = $userStorage->filter(['between', 'rank', 4, 5])->fetch()->one();
```

Running the tests
-----------------

[](#running-the-tests)

For tests, you need to connect to a database. If you use a docker, then you can raise the database with the following command:

```
docker-compose up -d mysql pgsql

```

And run tests:

```
docker-compose run --rm php codecept run --env pgsql --env mysql

```

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance12

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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.

### Community

Maintainers

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

---

Top Contributors

[![typomaker](https://avatars.githubusercontent.com/u/8394514?v=4)](https://github.com/typomaker "typomaker (42 commits)")

### Embed Badge

![Health badge](/badges/vivace-db/health.svg)

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

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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