PHPackages                             vakata/database - 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. vakata/database

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

vakata/database
===============

A database abstraction with support for various drivers.

5.5.1(5mo ago)37.8k↓75.6%15MITPHPPHP &gt;=8.0.0

Since Nov 29Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/vakata/database)[ Packagist](https://packagist.org/packages/vakata/database)[ Docs](https://github.com/vakata/database)[ RSS](/packages/vakata-database/feed)WikiDiscussions master Synced 2d ago

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

database
========

[](#database)

[![Latest Version on Packagist](https://camo.githubusercontent.com/9ec74f854fa064f18da48a4d076bfbcdd653ed63b69b612089db3bea949a94e7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f76616b6174612f64617461626173652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/vakata/database)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/eab0cae4499541606b356c42757e88cee8d73954f834b9e91d66fc6b99b34cbc/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f76616b6174612f64617461626173652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/vakata/database)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/4c6993807c946ed0f27c802fef3d899067499a58252f05f72a0162e88a0dfe68/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f76616b6174612f64617461626173652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/vakata/database)[![Code Coverage](https://camo.githubusercontent.com/0db9e1344954cc3739872839e6e7fd4f740e53c126a5f5983d86fdd7725d531a/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f76616b6174612f64617461626173652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/vakata/database)

A database abstraction with support for various drivers (mySQL, postgre, oracle, ibase, sqlite and even PDO).

Install
-------

[](#install)

Via Composer

```
$ composer require vakata/database
```

Usage
-----

[](#usage)

```
$db = new \vakata\database\DB('mysql://user:pass@127.0.0.1/database_name?charset=utf8');

// get an array result:
$db->all('SELECT id, name FROM table');
// [ [ 'id' => 1, 'name' => 'name 1' ], [ 'id' => 2, 'name' => 'name 2' ] ]

// passing parameters
$db->all('SELECT id, name FROM table WHERE id = ? OR id = ?', [ 1, 2]);
// [ [ 'id' => 1, 'name' => 'name 1' ], [ 'id' => 2, 'name' => 'name 2' ] ]

// if selecting a single column there is no wrapping array:
$db->all('SELECT name FROM table');
// [ 'name 1', 'name 2' ]

// setting a key for the resulting array
$db->all('SELECT id, name FROM table', null, 'id');
// [ 1 => [ 'id' => 1, 'name' => 'name 1' ], 2 => [ 'id' => 2, 'name' => 'name 2' ] ]

// skipping the key (which leaves a single column so it is not wrapped anymore)
$db->all('SELECT id FROM table', null, 'id', true);
// [ 1 => 'name 1', 2 => 'name 2' ]

// selecting a single row:
$db->one('SELECT id, name FROM table WHERE id = ?', [1]);
// [ 'id' => 1, 'name' => 'name 1' ]

// selecting a single value from a single row (no wrapping array):
$db->one('SELECT name FROM table WHERE id = ?', [1]);
// "name 1"

// insert / update / delete queries (affected rows count and last insert ID)
$db->query("UPDATE table SET name = ? WHERE id = ?", ['asdf', 1])->affected();
// 1
$db->query("INSERT INTO table (name) VALUES(?)", ['asdf'])->insertID();
// 3
$db->query("DELETE FROM table WHERE id = ?", [3])->affected();
// 1

// queries using the "all" method can also use the "get" method
// "get" does not create an array in memory, instead it fetches data from the mysql client
// the resulting object is not an array but can be iterated and supports indexes
// basically it can be used as an array as it implements all neccessary interfaces
foreach($db->get('SELECT id, name FROM table') as $v) {
    echo $v['id'] . ' ';
}
// 1 2
$db->get('SELECT id, name FROM table', null, 'id', true)[2];
// "name 2"

// SHORTCUT METHODS
// assuming there is a book table with a name column
foreach ($schema->book() as $book) {
    echo $book['name'] . "\n";
}
// you could of course filter and order
foreach ($schema->book()->filter('year', 2016)->sort('name') as $book) {
    // iterate over the books from 2016
}
// the same as above
foreach ($schema->book()->filterByYear(2016)->sortByName() as $book) {
    // iterate over the books from 2016
}

// if using mySQL or Oracle (others to come soon) foreign keys are automatically detected and can be fetched
// for example if there is an author table and the book table references it
foreach ($schema->book()->with('author') as $book) {
    echo $book['author']['name'] . "\n";
}

// provided there is a linking table book_tag and a tag table and each book has many tags you can do this
foreach ($schema->book()->with('tag') as $book) {
    echo $book['tag'][0]['name'] . "\n"; // the name of the first tag which the current book has
}

// filtering and ordering works on relations too
$schema->book()->filter('author.name', 'A. Name');

// you can also specify what fields to select
$schema->book()->select(['name', 'author.name']);

// there are "low-level" where / order and limit methods
$schema->book()
    ->with('author')
    ->where('created = CURDATE() OR promoted = ?', [1])
    ->order("author.name ASC")
    ->limit(5)
    ->select();

// when not dealing with foreign keys you can also join tables (and use group by / having)
$schema->categories()
    ->join('questions', [ 'category_id' => 'id' ])
    ->group(['id'])
    ->having('cnt > ?', [300])
    ->order('cnt DESC')
    ->limit(2)
    ->select(['id', 'cnt' => 'COUNT(questions.id)'])

// you can also insert / update or delete records
$schema->book()->insert(['name' => 'Book title']);
$schema->book()->where('id = 5')->update(['name' => 'New title']);
$schema->book()->where('id = 5')->delete();
```

Testing
-------

[](#testing)

```
$ composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [vakata](https://github.com/vakata)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

55

—

FairBetter than 97% of packages

Maintenance73

Regular maintenance activity

Popularity26

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity88

Battle-tested with a long release history

 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.

###  Release Activity

Cadence

Every ~16 days

Recently: every ~27 days

Total

223

Last Release

154d ago

Major Versions

1.4.0 → 2.0.02017-01-24

2.0.1 → 3.0.02017-04-05

3.30.0 → 4.0.02022-11-02

4.10.8 → 5.0.02024-02-19

PHP version history (4 changes)1.0.0PHP &gt;=5.4.0

3.12.0PHP &gt;=7.2.0

3.27.0PHP &gt;=7.4.0

4.0.0PHP &gt;=8.0.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/146052?v=4)[Ivan Bozhanov](/maintainers/vakata)[@vakata](https://github.com/vakata)

---

Top Contributors

[![vakata](https://avatars.githubusercontent.com/u/146052?v=4)](https://github.com/vakata "vakata (333 commits)")

---

Tags

databasevakata

### Embed Badge

![Health badge](/badges/vakata-database/health.svg)

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

###  Alternatives

[doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.

9.7k605.0M6.8k](/packages/doctrine-dbal)[doctrine/orm

Object-Relational-Mapper for PHP

10.2k300.5M7.5k](/packages/doctrine-orm)[doctrine/doctrine-bundle

Symfony DoctrineBundle

4.8k254.4M4.1k](/packages/doctrine-doctrine-bundle)[doctrine/migrations

PHP Doctrine Migrations project offer additional functionality on top of the database abstraction layer (DBAL) for versioning your database schema and easily deploying changes to it. It is a very easy to use and a powerful tool.

4.8k217.3M546](/packages/doctrine-migrations)[doctrine/data-fixtures

Data Fixtures for all Doctrine Object Managers

2.9k143.6M586](/packages/doctrine-data-fixtures)[robmorgan/phinx

Phinx makes it ridiculously easy to manage the database migrations for your PHP app.

4.5k48.7M460](/packages/robmorgan-phinx)

PHPackages © 2026

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