PHPackages                             denbeke/orm - 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. denbeke/orm

ActiveLibrary

denbeke/orm
===========

Simple ORM implementation for PHP

48PHP

Since Feb 2Pushed 10y ago1 watchersCompare

[ Source](https://github.com/DenBeke/ORM)[ Packagist](https://packagist.org/packages/denbeke/orm)[ RSS](/packages/denbeke-orm/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

ORM
===

[](#orm)

[![Build Status](https://camo.githubusercontent.com/f0638fbbf8c332be0a3aecf297398ea7636ac03e2db83eaccd1c99f89d73e082/68747470733a2f2f7472617669732d63692e6f72672f44656e42656b652f4f524d2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/DenBeke/ORM)

*Simple ORM implementation for PHP*

When working with SQL databases, there is always the same monkey work that has to be done, like selecting using the table parameters, putting the result back in to a class, ...

ORM (object-relational mapping) solves this problem, you can just call some methods on classes, using the properties defined in your classes.

This implementation is naive and very simple, but can spare you a lot of time and work.

---

- [Install](#install)
- [Usage](#usage)
    - [Configuration &amp; Initialization](#configuration--initialization)
    - [ORM class](#orm-class)
    - [Get methods](#get-methods)
        - [get()](#get)
        - [getBy\*()](#getby)
        - [Options](#options)
            - [limit](#limit)
            - [orderBy](#orderby)
            - [AND](#and)
            - [OR](#or)
    - [add()](#add)
    - [update()](#update)
    - [remove()](#remove)
- [License](#license)
- [Author](#author)

Install
-------

[](#install)

Since writing queries is a task I don't always enjoy, this ORM packages depends on a query builder: [Pixie](https://github.com/usmanhalalit/pixie) (by Muhammad Usman).

Dependencies are installed through [Composer](https://getcomposer.org):

```
$ composer install

```

You can of course also install this package using Composer:
Add `"denbeke/orm": "dev-master"` to your requirements and add the following code to the root of the `composer.json` file to add the Github repo:

```
"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/DenBeke/ORM.git"
    }
],
```

Usage
-----

[](#usage)

### Configuration &amp; Initialization

[](#configuration--initialization)

*Before you can do anything you must call the `\DenBeke\ORM\ORM::init()` function (you must do this once in your application). An exception will be thrown if you call a method on an uninitialized ORM class.*
`\DenBeke\ORM\ORM::init()` takes an associative PHP array as argument. This array must contain some basic database configuration.

```
$db_config = [
    'driver'    => 'mysql', // Db driver
    'host'      => 'localhost',
    'database'  => 'my_database_name',
    'username'  => 'root',
    'password'  => 'root',
];
\DenBeke\ORM\ORM::init($db_config);
```

The `$db_config` array will be passed to Pixie query builder. This means you can use all Pixie configuration options.

### ORM class

[](#orm-class)

Creating an "ORM-ready" class is very simple, just inherit from `\DenBeke\ORM\ORM`.

```
class Person extends \DenBeke\ORM\ORM {

    public $id;
    public $name;
    public $city;

}
```

After writing the code, you must also add a table `person` to the database, with the fields `id`, `name`, `city`.
As you may have noticed, the table name is derived from the Class name (without namespaces and converted to lowercase!) and the column names are just the names of the PHP fields.

The ORM implements a default constructor which takes an associative array or an stdClass and assigns the values from the input to the fields of the new object.

```
$person = new Person(['name' => 'Bob', 'city' => 'Amsterdam']);
```

### Get methods

[](#get-methods)

Once the class inherits from `\DenBeke\ORM\ORM` you can access all the ORM methods. Starting with the get methods.

#### get()

[](#get)

The predefined `get()` method fetches all records of the given type from the database.
If we have 3 records in the table `person` we can get all of them using the `get()` function:

```
$persons = Person::get();
```

In this example `$persons` is an array of size 3, containing objects of type `Person`.

#### getBy\*()

[](#getby)

Whenever a class has a field, the caller can get records from the database by those fields. In this example the class Person has the fields `id`, `name`, `city`. So you can call the following functions:

- `Person::getById($id);`
- `Person::getByName($name);`
- `Person::getByCity($city);`

Those static functions will return an array of Person elements, where the input parameter matches the table column.

#### Options

[](#options)

*Work In Progress*

You can supply options to the `get()` and `getBy*()` methods. The options parameter is an associative array.

```
$options = [
    'option1' => '...',
    'option2' => '...'
];
```

##### limit

[](#limit)

```
$persons = Person::get(['limit' => 4]);
```

##### orderBy

[](#orderby)

You can order your results by supplying the `orderBy` option. `orderBy` should be an array, containing the the database column/field to order by, and an optional direction (`ASC` or `DESC', default `ASC`).

If you have e.g. *Bob*, *John*, *Alice* in your database, the following operation will return *Alice*, *Bob*, *John*.

```
$options = [
    'orderBy' => ['name', 'ASC'],
];

$persons = Person::get($options);
```

##### AND

[](#and)

Adding `AND` clause to the `getBy*` method can be done using the `andWhere` option.

```
$options = [
    'andWhere' => [
        'age',
        '>',
        18
    ]
];

Person::getByName($name, $options);
```

##### OR

[](#or)

Adding `OR` clause to the `getBy*` method can be done using the `orWhere` option.

```
$options = [
    'orWhere' => [
        'city',
        '=',
        'Antwerp'
    ]
];

Person::getByCity('Brussels', $options);
```

### add()

[](#add)

Adding records to the database is quite simple, just create an instance of the class and call the `add()` method on it.

```
$person = new Person;
$person->name = 'Alice';
$person->city = 'Brussels';

$person->add();
```

### update()

[](#update)

Updating a record is as easy as adding records.
Just alter a field and call the `update()` method.

```
$person = Person::getByName('Bob')[0];
$person->city = 'Brussels';

$person->update();
```

### remove()

[](#remove)

Deleting records can be done using the `remove()` method.

```
$person = Person::getById(3)[0];

$person->remove();
```

License
-------

[](#license)

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see .

Author
------

[](#author)

Mathias Beke - [denbeke.be](http://denbeke.be/)

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

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/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?d=identicon)[DenBeke](/maintainers/DenBeke)

---

Top Contributors

[![DenBeke](https://avatars.githubusercontent.com/u/3856745?v=4)](https://github.com/DenBeke "DenBeke (45 commits)")

### Embed Badge

![Health badge](/badges/denbeke-orm/health.svg)

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

PHPackages © 2026

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