PHPackages                             j0113/odb - 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. j0113/odb

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

j0113/odb
=========

Object Oriented database wrapper for PHP. Designed to be(come) compatible with multiple database. Queries are written in PHP and tables are connected to models.

v1.2.1(5y ago)167Apache-2.0PHPPHP &gt;=7.4

Since Feb 19Pushed 5y ago1 watchersCompare

[ Source](https://github.com/J0113/ODB)[ Packagist](https://packagist.org/packages/j0113/odb)[ RSS](/packages/j0113-odb/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)DependenciesVersions (6)Used By (0)

J0113/ODB
=========

[](#j0113odb)

Object-oriented database wrapper for PHP. Designed to be(come) compatible with multiple database. Queries are written in PHP and tables are connected to models.

Prerequisites/Requirements
--------------------------

[](#prerequisitesrequirements)

- PHP &gt;=7.4 (for now)
- A MySQL database (with tables)
- PDO

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

[](#installation)

J0113/ODB can be installed using composer.

```
composer require j0113/odb *

```

Getting started
---------------

[](#getting-started)

**1. Database connection**

The first thing you want to do is set up the connection to your database. This will setup the database connection for the whole application.

```
use J0113\ODB\PDODatabase;
PDODatabase::connect("server.localhost", "myusername", "mypassword", "mydatabase");
```

**2. Mapping a model**

All models must extend `J0113\ODB\PDODatabaseObject`.

```
use J0113\ODB\PDODatabaseObject;
class User extends PDODatabaseObject
{
    // First you may define a table (defaults to the className)
    protected const TABLE = "users";

    // Than some fields:
    protected ?string $username = null;
    protected ?string $firstname = null;
    protected ?string $lastname = null;

    // Maybe some relations (more about that later)
    protected const RELATIONS = [
        "orders" => ["toOne", "Order", "user_id", "id"],
    ];

    // And getters and setters
    public function getUsername() : ?string
        ...
}
```

That's it, now you can access your database from PHP in an object-oriented way.

Usage
-----

[](#usage)

**1. Inserting**

```
$user = new User();
$user->setFirstname("John");
$user->setLastname("Smith");
$user->setUsername("john_smith");

$user->save();

echo $user->getId();
```

**2. Retrieving data**

To get data you must use the `J0113\ODB\QueryBuilder`, if more databases are supported queries will be supported.

```
use J0113\ODB\QueryBuilder;

/**
 * Get some users and display info
 * @param QueryBuilder
 * @return User[]|null
 */
$users = User::get(
    (new QueryBuilder())
        ->where("firstname", "John")
        ->andWhere("lastname", "Smith")
        ->limit(5)
);

if (!empty($users)){
    foreach ($users as $user){
        echo "Hello " . $user->getFirstname() . "!\n";
    }
}

/**
 * Get one user
 * @param QueryBuilder
 * @return User|null
 */
$user = User::getOne(
    (new QueryBuilder())->where("username", "oliver12345")
);
```

**3. Updating a row**

To update data the proces is about the same, just leave the ID as is and use `save()`.

```
use J0113\ODB\QueryBuilder;

/**
 * Get one user
 * @param QueryBuilder
 * @return User|null
 */
$user = User::getOne(
    (new QueryBuilder())->where("username", "oliver12345")
);
$user->setFirstname("Oli");
$user->save();
```

Relations
---------

[](#relations)

One issue with mapping a database to PHP objects is when relations come into play. ODB uses takes a JIT approach, the related object is 'accessible' from PHP but only when it gets accessed the query will get executed.

Releations are defined in the model object using the `protected const RELATIONS` array, relations can be `toOne` or `toMany`.

```
use J0113\ODB\PDODatabaseObject;
use J0113\ODB\QueryBuilder;
class User extends PDODatabaseObject
{
    /**
     * Stores all the relations, these are read only and results in more than one query IF the relation is queried.
     * Must be an sub array with "key" => ["type", "class", "column", "property"].
     * - Key: is under what $obj->key the relation can be accessed.
     * - Type: can be toOne (this-1) or toMany (this-multiple)
     * - Class: the class it should be connected to (must be an child of PDODatabaseObject)
     * - Column: the column it should search in
     * - Property: the value to match against, is a property (or variable) of the current object
     *
     * SQL will generated by:
     * - LIMIT: Type
     * - FROM: Class
     * - WHERE: column = this->property
     *
     * protected const RELATIONS = ["customer" => ["toOne", "Model\User", "id", "user_id"] ];
     * $order->customer = Model\User(...)
     *
     * Tip: Use the PHPDoc '@ property' to let the IDE know about the relation.
     *
     * @var array
     */
    protected const RELATIONS = [
        "orders" => ["toMany", "Order", "user_id", "id"]
        // $this->orders would result in Order::get((new QueryBuilder())->where("user_id", $this->id));
    ];

    /**
     * You may use a getter
     * @return Order[]
     */
    public function getOrders() : array
    {
        return $this->orders;
    }
}

$user = User::getOne((new QueryBuilder()));
$user_orders = $user->getOrders();
```

License
-------

[](#license)

`J0113/ODB` is released under the Apache 2.0 license. See the enclosed `LICENSE` for details.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity56

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

Every ~9 days

Total

5

Last Release

1873d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/70b943d77743b2eee80992590e363fc4f3ae505d1ab237624100bea2c3896419?d=identicon)[J0113](/maintainers/J0113)

---

Top Contributors

[![J0113](https://avatars.githubusercontent.com/u/11798875?v=4)](https://github.com/J0113 "J0113 (17 commits)")

### Embed Badge

![Health badge](/badges/j0113-odb/health.svg)

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

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