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

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

apex/db
=======

Light weight, straight forward DB interface

2.1(2y ago)11.0k13MITPHPPHP &gt;=8.1

Since Mar 5Pushed 2y ago2 watchersCompare

[ Source](https://github.com/apexpl/db)[ Packagist](https://packagist.org/packages/apex/db)[ Docs](https://apexpl.io)[ RSS](/packages/apex-db/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (5)Versions (44)Used By (3)

ADL - Apex Database Layer
=========================

[](#adl---apex-database-layer)

A lightweight database layer designed for simplicity and ease of use, providing a middle ground between larger ORMs and base PHP functions (ie. mysqli\_\*). Supports multiple database engines, object mapping, initialization wrappers for Doctrine and Eloquent, connection load balancing, and an optional wrapper allowing methods to be accessed statically. It supports:

- Supports mySQL, PostgreSQL, and SQLite with ability to easily implement other engines.
- Automated mapping to / from objects.
- Automated preparing of ALL sql queries to protect against SQL injection.
- Typed, sequential and named placeholders
- Initialization wrappers for Doctrine and Eloquent
- Optional secondary read-only connection parameters, which automatically switch to write connection when necessary SQL query is executed.
- Optional redis support with connection manager allowing both, easy maintainability of connection information across multiple server instances, and multiple read-only connections with automated load balancing via round robin.
- Command line tool (and PHP class) to easily manage connection information within redis.
- Optional built-in support for [Apex Debugger](https://github.com/apexpl/debugger) which will log all SQL queries executed during a request into the debug session for later analysis.
- Wrapper class allowing methods to be accessed statically for improved efficiency and simplicity.

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

[](#installation)

Install via Composer with:

> `composer require apex/db`

Table of Contents
-----------------

[](#table-of-contents)

1. [Database Connections](https://github.com/apexpl/db/blob/master/docs/connections.md)
2. [SQL Database Methods (26 methods)](https://github.com/apexpl/db/blob/master/docs/sql.md)
    1. [insert()](https://github.com/apexpl/db/blob/master/docs/sql/insert.md)
    2. [update()](https://github.com/apexpl/db/blob/master/docs/sql/update.md)
    3. [insertOrUpdate()](https://github.com/apexpl/db/blob/master/docs/sql/insertOrUpdate.md)
    4. [query()](https://github.com/apexpl/db/blob/master/docs/sql/query.md)
    5. [getRow()](https://github.com/apexpl/db/blob/master/docs/sql/getRow.md)
    6. [getIdRow()](https://github.com/apexpl/db/blob/master/docs/sql/getIdRow.md)
    7. [getObject()](https://github.com/apexpl/db/blob/master/docs/sql/getObject.md)
    8. [getIdObject()](https://github.com/apexpl/db/blob/master/docs/sql/getIdObject.md)
    9. [getField()](https://github.com/apexpl/db/blob/master/docs/sql/getField.md)
    10. [getColumn()](https://github.com/apexpl/db/blob/master/docs/sql/getColumn.md)
    11. [getHash()](https://github.com/apexpl/db/blob/master/docs/sql/getHash.md)
3. [Placeholders](https://github.com/apexpl/db/blob/master/docs/placeholders.md)
4. [Object Mapping](https://github.com/apexpl/db/blob/master/docs/object_mapping.md)
5. Additional / Supplemental
    1. [SQL Parser for Large SQL Files](https://github.com/apexpl/db/blob/master/docs/sql_parser.md)
    2. [Using redis and the Connection Manager](https://github.com/apexpl/db/blob/master/docs/connect_mgr.md)
    3. [Utilizing Apex Debugger](https://github.com/apexpl/db/blob/master/docs/debugger.md)
    4. [Db Wrapper for Statically Accessing Methods](https://github.com/apexpl/db/blob/master/docs/static_wrapper.md)
    5. [Initialization Wrappers for Doctrine, Eloquent, and PDO](https://github.com/apexpl/db/blob/master/docs/wrappers.md)

Basic Usage
-----------

[](#basic-usage)

```
use Apex\Db\Drivers\mySQL\mySQL;

$db = new mySQL([
    'dbname' => 'mydb',
    'user' => 'myuser',
    'password' => 'mydb_password'
]);

// Insert a record
$db->insert('users', [
    'username' => 'jsmith',
    'full_name' => 'John Smith',
    'email' => 'jsmith@domain.com'
);
$userid = $db->insertId();

// Get single user by id#
if (!$profile = $db->getIdRow('users', $userid)) {
    die("No user at id# $userid");
}

// Get single field
if (!$email = $db->getField("SELECT email FROM users WHERE id = %i", $userid)) {
    die("No e-mail exists for user id# $userid");
}
echo "E-mail is: $email\n";

// Go through all users with @domain.com e-mail
$domain = '@domain.com';
$rows = $db->query("SELECT * FROM users WHERE email LIKE %ls", $domain);
foreach ($rows as $row) {
    echo "Found: $row[username] - $row[full_name]\n";
}
```

Object Mapping
--------------

[](#object-mapping)

Allows mapping to and from objects with ease by simply passing the objects to write methods, and one static call to map results to an object. For example:

```
use Apex\Db\Drivers\mySQL\mySQL;
use Apex\Db\Mapper\ToInstance;
use MyApp\Models\UserModel;

$db = new mySQL([
    'dbname' => 'mydb',
    'user' => 'myuser',
    'password' => 'mydb_password'
]);

// Get users
$rows = $db->"SELECT * FROM users WHERE group_id = 2");
foreach ($rows as $row) {
    $user = ToInstance::map(UserModel::class, $row);
    // $user is a UserModel object, injected and instantiated
}

// Get specific user
$userid = 5811;
$row = $db->getIdRow('users', $userid);    /// $user  is a UserModel object
$user = ToInstance::map(UserModel::class, $row);

// Insert new user
$user = new UserModel($my, $constructor, $params);
$db->insert('users', $user);

// Unsure if inserting or updating?  No problem.
$db->insertOrUpdate('users', $user);
```

Follow Apex
-----------

[](#follow-apex)

Loads of good things coming in the near future including new quality open source packages, more advanced articles / tutorials that go over down to earth useful topics, et al. Stay informed by joining the [mailing list](https://apexpl.io/) on our web site, or follow along on Twitter at [@mdizak1](https://twitter.com/mdizak1).

###  Health Score

35

—

LowBetter than 81% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 72.3% 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 ~23 days

Recently: every ~36 days

Total

43

Last Release

884d ago

Major Versions

0.3.12 → 2.02022-01-30

PHP version history (2 changes)0.1PHP &gt;=8.0.0

2.0PHP &gt;=8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/4fe486d2ed7db571c0519bb0d52b08b5e953a911936e87331e736a381ef96f29?d=identicon)[apex](/maintainers/apex)

---

Top Contributors

[![mdizak](https://avatars.githubusercontent.com/u/59886259?v=4)](https://github.com/mdizak "mdizak (34 commits)")[![apexpl](https://avatars.githubusercontent.com/u/56146521?v=4)](https://github.com/apexpl "apexpl (12 commits)")[![LazySoftware13](https://avatars.githubusercontent.com/u/79599452?v=4)](https://github.com/LazySoftware13 "LazySoftware13 (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[knplabs/doctrine-behaviors

Doctrine Behavior Traits

93612.6M55](/packages/knplabs-doctrine-behaviors)[rami/entity-kit-bundle

A modern Symfony bundle that gives your Doctrine entities superpowers. It provides a growing collection of prebuilt behaviors — like Timestamp, Slugging, auditing, and more — as modular, composable traits or attributes.

101.2k](/packages/rami-entity-kit-bundle)

PHPackages © 2026

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