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

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

martynbiz/database
==================

Collection of database classes to allow database operations and unit testing

014PHP

Since Nov 30Pushed 11y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

\#Database

Light weight ORM classes. It's designed to be well suited for unit testing where the database adapter can be mocked easily. Also provides just enough to do common tasks such as find/select/create/update/delete, and associations between tables (e.g. hasMany/ belongsTo). Can be run stand along or within a framework.

\##Installation

Install with composer:

```
"martynbiz/database": "dev-master"

```

\##ORM

Models/User.php

```
class User extends MartynBiz\Database\Table
{
    protected $table = 'users';
}
```

Script

```
$adapter = new MartynBiz\Database\Adapter(array(
    'dsn' => '...',
    'user' => '...',
    'password' => '...',
));

$usersTable = Users::getInstance($adapter);

// return Row object
$user = $usersTable->find($id);

// select many rows by query (with options)
$where = 'age > 34';
$options = array(
    'limitStart' => 10,
    'limitMax' => 25,
);
$users = $usersTable->select($where, $options);

// insert new row
$values = array(
    'name' => 'Hugo',
    'age' => 54,
);
$users->create($values);

// update rows
$usersTable->update($values, $where, $options);

// delete rows
$usersTable->delete($where, $options);
```

\###Associations

```
class User extends Table
{
    protected $tableName = 'users';

    protected $hasMany = array(
        'transactions' => array(
            'table' => 'App\Model\Transaction', // class name for the hasMany rows
            'foreign_key' => 'user_id',
        )
    );
}

class Transaction extends Table
{
    protected $tableName = 'transactions';

    protected $belongsTo = array(
        'user' => array(
            'table' => 'App\Model\User', // class name for the belongsTo row
            'foreign_key' => 'user_id',
        )
    );
}
```

\##Database adapter

...

\##Unit testing

\###Table

```
$adapterMock = $this->getMockBuilder('MartynBiz\Database\Adapter')
    ->disableOriginalConstructor()
    ->getMock();

$usersTable = new Users($adapterMock);
```

\###Adapter

If it was neccessary to extend the adapter class, the PDO object which is usually generated internally can be injected instead (upon which the database credentials will be ingored). This allows the extended class to be unit testing with a mock PDO object.

```
class MyCustomAdapter extands Adapter {

}

$PDOMock->expects( $this->once() )
    ->method('prepare')
    ->with($sql);
$PDOStatementMock->expects( $this->once() )
    ->method('execute')
    ->with($whereValues);
$PDOStatementMock->expects( $this->once() )
    ->method('fetchAll')
    ->will( $this->returnValue($mockExecuteResult) );

// create instance with mock
$adapter = new MyCustomAdapter(null, $PDOMock);
```

TODO

- cascade
-

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

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/c1a27a170ef279c7363e50281f05711095a652ec6489eb060872022a9887ce8a?d=identicon)[martynbiz](/maintainers/martynbiz)

---

Top Contributors

[![martynbiz](https://avatars.githubusercontent.com/u/6215352?v=4)](https://github.com/martynbiz "martynbiz (18 commits)")

### Embed Badge

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

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

###  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)
