PHPackages                             gamernetwork/yolk-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. [Framework](/categories/framework)
4. /
5. gamernetwork/yolk-database

AbandonedArchivedLibrary[Framework](/categories/framework)

gamernetwork/yolk-database
==========================

Gamer Network's PHP database abstraction library

v1.0(10y ago)08.4k1MITPHPPHP &gt;=5.4.0

Since Jul 24Pushed 10y ago9 watchersCompare

[ Source](https://github.com/gamernetwork/yolk-database)[ Packagist](https://packagist.org/packages/gamernetwork/yolk-database)[ RSS](/packages/gamernetwork-yolk-database/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (3)Used By (0)

Want to work for Gamer Network? [We are hiring!](http://www.gamesindustry.biz/jobs/gamer-network)

Yolk Database
=============

[](#yolk-database)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/c45321ba9c87ebb29887aa67e0bb77a1a97b78e53b382914819375544780201c/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f67616d65726e6574776f726b2f796f6c6b2d64617461626173652f6261646765732f7175616c6974792d73636f72652e706e673f623d646576656c6f70)](https://scrutinizer-ci.com/g/gamernetwork/yolk-database/?branch=develop)

A simple database abstraction layer that provides a lightweight wrapper around PDO for ease-of-use. It currently supports MySQL, Postgres and SQLite.

Also included are simple query generators and a class for handling a tree structure within a relational database via modified preorder tree traversal.

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

[](#requirements)

This library requires only PHP 5.4 or later and the Yolk Contracts package (`gamernetwork/yolk-contracts`).

Installation
------------

[](#installation)

It is installable and autoloadable via Composer as `gamernetwork/yolk-database`.

Alternatively, download a release or clone this repository, and add the `\yolk\database` namespace to an autoloader.

License
-------

[](#license)

Yolk Database is open-sourced software licensed under the MIT license

Quick Start
-----------

[](#quick-start)

```
use yolk\database\DSN;
use yolk\database\adapters\MySQLConnection;

// create a DSN
$dsn = DSN::fromString('mysql://localhost/mydb');

// create a connection instance
$db = new MySQLConnection($dsn);

// get some data
$user = $db->getAssoc("SELECT * FROM users WHERE user_id = ?", 123);

// update some data
$updated = $db->execute(
    "UPDATE users SET last_seen = :now WHERE id = :id",
    [
        'id'  => 123,
        'now' => date('Y-m-d H:i:s'),
    ]
);
```

DSNs
----

[](#dsns)

A DSN is an object that specifies the properties of a database connection.

Common properties are:

- `type` - the type of database to connect to (mysql, postgres or sqlite)
- `host` - the host to connect to
- `port` - the port number to connect on
- `user` - the user to authenticate as
- `pass` - the user's password
- `db` - the name of the database schema to connect to
- `options` - an array of driver specific options

DSNs can be created by passing an array of properties to the constructor:

```
$dsn = new DSN([
	'type' => 'mysql',
	'host' => 'localhost',
	'db'   => 'myapp',
]);
```

or by calling the static `fromString()` method with a URI:

```
$dsn = DSN::fromString('mysql://root:abc123@myapp.db/myapp?charset=utf-8');
```

ConnectionManager
-----------------

[](#connectionmanager)

The ConnectionManager is a service to handle multiple database connections. A client can register a connection or DSN under a specific name and retrieve the connection at a later time.

When a DSN is registered, a suitable connection object is created automatically.

```
use yolk\database\ConnectionManager;
use yolk\database\adapters\SQLiteConnection;

// create a ConnectionManager instance
$m = new ConnectionManager();

// register a DSN
$m->add('mydb1', 'mysql://localhost/mydb');

// register an existing connection
$db = new SQLiteConnection('sqlite://var/www/myapp/myapp.db');
$m->add('mydb2', $db);

// determine if a connection with the specified name exists
$exists = $m->has('mydb1');

// retrieve a previously added connection
$db = $m->get('mydb1');

// remove a connection from the manager and return it
// NOTE: this does not disconnect the connection
$db = $m->remove('mydb1');
```

Query Method Reference
----------------------

[](#query-method-reference)

```
 // Execute a query and return the resulting PDO_Statement
$stmt = $db->query($statement, $params = []);

// Execute a query and return the number of affected rows
$rows = $db->execute($statement, $params = []);

// Execute a query and return all matching data as an array of associative arrays of matching rows
// Each row array has column names as keys
$db->getAll($statement, $params = []);

// Execute a query and return all matching data as an associative array,
// the first selected column is used as the array key
$db->getAssoc($statement, $params = []);

// Execute a query and return all matching data as a two-dimensioanl
// associative array, the first two selected columns are used as the array keys
$db->getAssocMulti($statement, $params = []);

// Execute a query and return the first matching row as an associative array
$db->getRow($statement, $params = []);

// Execute a query and return all values of the first selected column as an array
$db->getCol($statement, $params = []);

// Execute a query and return the value of the first column in the first array
$db->getOne($statement, $params = []);
```

The above methods accept the following parameters:

- `$statement`: a `PDO_Statement` instance or a SQL string
- `$params`: an array of parameters to bind to the statement

Query parameters may be bound name:

```
$user_id = $db->getOne(
    "SELECT id FROM user WHERE type = :type AND name LIKE :name",
    [
        'type' => 'NORMAL',
        'name' => 'Jim%',
    ]
);
```

or by position:

```
$user_id = $db->getOne(
    "SELECT id FROM user WHERE type = ? AND name LIKE ?",
    ['NORMAL', 'Jim%']
);
```

If the query has only a single parameter it may be specified directly and will be automatically converted to a positional parameter:

```
$user_id = $db->getOne("SELECT id FROM user WHERE login = ?", 'jimbob');
```

Other Methods
-------------

[](#other-methods)

```
// Returns the ID of the last inserted row or sequence value.
$id = $db->insertId($name = '');

// Escape/quote a value for use in a query string
$db->quote($value, $type = \PDO::PARAM_STR);

// Escape/quote an identifier name (table, column, etc)
// Allows reserved words to be used as identifiers.
$db->quoteIdentifier('key');

// Execute a raw SQL string and return the number of affected rows.
// Primarily used for DDL queries
$db->rawExec($sql);
```

Transactions
------------

[](#transactions)

```
// Begin a transaction
$db->begin();

// Commit the current transaction
$db->commit();

// Rollback the current transaction
$db->rollback();

// Determines if a transaction is currently active
$db->inTransaction();
```

Query Generators
----------------

[](#query-generators)

OO query generators are available for `SELECT`, `INSERT`, `UPDATE` and `DELETE`. An instance of each can be created by calling the corresponding method on the `DatabaseConnection`.

### Select

[](#select)

```
$db->select()

   // accepts true (default) or false as argument
   ->distinct()

   // comma-separated list or array of column names
   ->cols('*')

   // table to select from
   ->from('table')

   // append a where clause - column, operator, value
   // multiple calls add additional clauses
   ->where('created', '>=', '2016-01-01')

   // with two arguments, operator is assumed to be '='
   ->where('id', 123)

   // array of columns to group by
   ->groupBy(['type', 'status'])

   // second parameter specifies ascending (true) or descending (false)
   // multiple calls add additional clauses
   ->orderBy('column', true)

   // return result as associative array
   // can also use the other fetch* methods defined by DatabaseConnection
   ->fetchAssoc();
```

### Insert

[](#insert)

```
$db->insert()

   // accepts true (default) or false as argument
   ->ignore()

   // table to insert to
   ->into('table')

   // item to insert as an associative array of column names/values
   ->item([
       'col1' => 'value1',
       'col2' => 'value1',
   ])

   // run the query
   ->execute();
```

### Update

[](#update)

```
$db->insert()

   // accepts true (default) or false as argument
   ->ignore()

   // table to insert to
   ->into('table')

   // columns to update as an associative array of column names/values
   ->set([
       'col1' => 'value1',
       'col2' => 'value1',
   ])

   // same usage as for SELECT
   ->where('id', 123)

   // run the query
   ->execute();
```

### Delete

[](#delete)

```
$db->delete()

   // table to insert to
   ->from('table')

   // same usage as for SELECT
   ->where('id', 123)

   // run the query
   ->execute();
```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity59

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.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3951d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2658e540902cbc1f59ec171e1b74515fb9e7c3a1e194e37e9c37f27ae400e4b4?d=identicon)[gamer-network](/maintainers/gamer-network)

---

Top Contributors

[![simon-downes](https://avatars.githubusercontent.com/u/1052903?v=4)](https://github.com/simon-downes "simon-downes (10 commits)")

---

Tags

frameworkeurogamergamer network

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[hemp/presenter

Easy Model Presenters in Laravel

247592.6k1](/packages/hemp-presenter)[pestphp/pest-plugin-stressless

Stressless plugin for Pest

67792.6k16](/packages/pestphp-pest-plugin-stressless)[wpstarter/framework

The WpStarter Framework - Laravel Framework for WordPress

1810.1k4](/packages/wpstarter-framework)

PHPackages © 2026

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