PHPackages                             joefallon/phpdatabase - 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. joefallon/phpdatabase

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

joefallon/phpdatabase
=====================

This package contains simple to use database access layer.

v8.0.1(7mo ago)0129MITPHPPHP &gt;=7.4.0

Since Aug 29Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/joefallon/phpdatabase)[ Packagist](https://packagist.org/packages/joefallon/phpdatabase)[ Docs](https://github.com/joefallon/phpdatabase)[ RSS](/packages/joefallon-phpdatabase/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (1)Versions (21)Used By (0)

phpdatabase
===========

[](#phpdatabase)

By [Joe Fallon](http://blog.joefallon.net)

A simple library for MySQL database access. It has the following features:

- Full suite of unit tests.
- It can be integrated into any existing project.
- Can be fully understood in just a few moments.
- The library implements the data mapper design patter (a.k.a. table gateway).

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

[](#installation)

The easiest way to install PhpDatabase is with [Composer](https://getcomposer.org/). Create the following `composer.json` file and run the `php composer.phar install` command to install it.

```
{
    "require": {
        "joefallon/phpdatabase": "*"
    }
}
```

Usage
-----

[](#usage)

There are four main classes are are used to represent all of the relationships within a database:

- `AbstractTableGateway`
- `AbstractJoinTableGateway`
- `PdoFactory`

### Entities

[](#entities)

An entity is a class that represents a single row within a database. All entities must contain a `primary key` feild and optionally a `created at` and `updated at` field:

- `primary key` - This is the primary key of the row. It can be named anything.
- `created at` - This is the date and time that the row was created. It can be named anything.
- `updated at` - This is the date and time that the row was last updated. It can be named anything.

Additional data fields are added to the entity. Each data field that is added to the entity and should correlate one-to-one with columns within a given table.

### Abstract Table Gateway

[](#abstract-table-gateway)

Instances of subclasses of `AbstractTableGateway` are used to mediate all access to a table within the database.

Each subclass must implement the abstract methods `mapObjectToArray` and `mapArrayToObject`. The method `mapObjectToArray`is used to convert an entity to an associative array. The names of the keys map to the column names within the database. The method `mapArrayToObject` is used to convert an associative array that was retrieved from the database into an object.

Additionally, several methods are provided to assist with access to the database. There are four major methods that are used to provide the basic CRUD (i.e. Create, Retrieve, Update, Delete) access to the database. The the following methods are used to provide public access:

- `baseCreate($entity)`
- `baseRetrieve($id)`
- `baseUpdate($entity)`
- `baseDelete($id)`

### Abstract Join-Table Gateway

[](#abstract-join-table-gateway)

The class `AbstractJoinTableGateway` is used mediate access to join tables (i.e. junction table). These tables to represent many-to-many associations.

### PDO Factory

[](#pdo-factory)

The `PdoFactory` factory class is used to create a PHP `PDO` object.

### Usage

[](#usage-1)

Please refer to the [unit tests](https://github.com/joefallon/phpdatabase/tree/master/tests)for a detailed example of how to use this package.

Testing
-------

[](#testing)

This project uses PHPUnit for unit testing. Tests are written to be compatible with PHP 7.4+ and the project pins PHPUnit to the 9.6 series (the last major version that supports PHP 7.4).

- Install development dependencies and the test runner with:

    composer install
- Run the test suite from the project root with:

    ./vendor/bin/phpunit

PHPUnit configuration is provided in `phpunit.xml` and the test bootstrap is `tests/bootstrap.php`.

If you need to run a single test class or file, pass the path to PHPUnit, for example:

```
./vendor/bin/phpunit tests/JoeFallon/PhpDatabase/ExampleEntityTests.php

```

Notes
-----

[](#notes)

- The project previously used a small, custom test framework; it has been migrated to PHPUnit and all tests have been updated to use PHPUnit APIs.
- The library targets PHP &gt;= 7.4. If you upgrade PHP beyond 7.4, consider updating PHPUnit to a newer major version and adjusting tests as needed.

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance64

Regular maintenance activity

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity72

Established project with proven stability

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

Recently: every ~888 days

Total

19

Last Release

218d ago

Major Versions

v3.0.2 → v4.0.02015-09-29

v4.0.1 → v5.0.02016-01-02

v5.0.2 → v6.0.02016-01-25

v6.0.0 → v7.0.02016-07-20

v7.0.0 → v8.0.02025-10-11

PHP version history (2 changes)v1.0.0PHP &gt;=5.3.0

v8.0.0PHP &gt;=7.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/97cecfa80ffec8e9e7b5d0ff6df026b636e31a15fe0c2155baba402b5d4f0819?d=identicon)[joefallon](/maintainers/joefallon)

---

Top Contributors

[![joefallon](https://avatars.githubusercontent.com/u/4212989?v=4)](https://github.com/joefallon "joefallon (36 commits)")

---

Tags

database

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/joefallon-phpdatabase/health.svg)

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

###  Alternatives

[doctrine/dbal

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

9.7k578.4M5.6k](/packages/doctrine-dbal)[doctrine/orm

Object-Relational-Mapper for PHP

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

Symfony DoctrineBundle

4.8k241.3M3.3k](/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.8k204.8M440](/packages/doctrine-migrations)[doctrine/data-fixtures

Data Fixtures for all Doctrine Object Managers

2.9k136.1M516](/packages/doctrine-data-fixtures)[robmorgan/phinx

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

4.5k46.2M405](/packages/robmorgan-phinx)

PHPackages © 2026

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