ORM 1.0.0 · Library for PHP
Rows as PHP objects, mapped by classes
ORM maps a database to PHP classes through inheritance: one class names the schema, a subclass the table, and another receives the rows. Insert an object and read back what the database filled in, load a table as a list of objects, and save or delete a row by its identity. Column names in snake_case become properties in camelCase.
Free software (GPL 3) · PHP 7.1 or newer · PDO with PostgreSQL · one class
- new Person([...])an object, no id yet
- insert()INSERT ... RETURNING *
- The object#1 Ada, created 2026-01-01
Examples
What sets it apart
Each example is a complete script, run with ORM 1.0.0. The output is what it printed.
Classes map the tables
Person receives rows of shop.person. Fields marked AUTOMATIC are left to the database and come back after the insert.
<?php require __DIR__ . '/../connect.php'; use divengine\orm; // The mapping is a class hierarchy: a schema, a record of a table in it, // and the class that receives the rows. class Shop extends orm { protected $__map_type = self::SCHEMA; protected $__map_schema = 'shop'; protected $__map_identity = 'id = :id'; } class PersonMap extends Shop { protected $__map_name = 'person'; protected $__map_class = Person::class; } class Person extends PersonMap { protected $__map_type = self::RECORD; public $id = self::AUTOMATIC; // the database gives it public $firstName; // column first_name public $email; public $created = self::AUTOMATIC; // so is this one (a default) } class People extends PersonMap { protected $__map_type = self::TABLE; } // Insert: the row comes back with what the database filled in $ada = new Person(['first_name' => 'Ada', 'email' => 'ada@example.com']); $ada->insert(); echo "Inserted: #$ada->id $ada->firstName, created $ada->created\n"; (new Person(['first_name' => 'Linus']))->insert(); (new Person(['first_name' => 'Grace', 'email' => 'grace@example.com']))->insert(); // A table map loads rows as Person objects $people = new People(); $people->load(); echo 'Loaded ', $people->count(), " people:\n"; $people->each(function (Person $p) { printf(" #%d %-6s %s\n", $p->id, $p->firstName, $p->email ?? '(no email)'); }); $found = $people->getFirstItem('email LIKE ?', ['grace@%']); echo 'First with a grace@ email: ', get_class($found), " #$found->id $found->firstName\n";
Inserted: #1 Ada, created 2026-01-01 Loaded 3 people: #1 Ada ada@example.com #2 Linus (no email) #3 Grace grace@example.com First with a grace@ email: Person #3 Grace
Save and delete by identity
The identity, id = :id, finds the row again to update or delete it.
<?php require __DIR__ . '/../connect.php'; use divengine\orm; class Shop extends orm { protected $__map_type = self::SCHEMA; protected $__map_schema = 'shop'; protected $__map_identity = 'id = :id'; // how a row is found again } class Person extends Shop { protected $__map_type = self::RECORD; protected $__map_name = 'person'; public $id = self::AUTOMATIC; public $firstName; public $email; public $created = self::AUTOMATIC; } class People extends Shop { protected $__map_type = self::TABLE; protected $__map_name = 'person'; protected $__map_class = Person::class; } foreach (['Ada', 'Linus', 'Grace'] as $name) { (new Person(['first_name' => $name]))->insert(); } $people = new People(); $grace = $people->getFirstItem('first_name = ?', ['Grace']); $grace->email = 'grace@example.com'; $grace->save(); // UPDATE shop.person ... WHERE id = :id $linus = $people->getFirstItem('first_name = ?', ['Linus']); $linus->delete(); // DELETE FROM shop.person WHERE id = :id foreach ($people->getAll('id > ?', [0]) as $p) { printf("#%d %-6s %s\n", $p->id, $p->firstName, $p->email ?? '(no email)'); }
#1 Ada (no email) #3 Grace grace@example.com
Install
Add it to a project
1. Require it
With Composer, then create the database the examples use.
composer require divengine/orm:1.0.0 createdb divengine_orm_examples
Save one of the scripts above in an examples folder, next to connect.php, and run it:
php examples/records.php
2. Connect
Every example loads this file first. It connects to PostgreSQL and makes the table again, so each run starts empty.
<?php // Shared by the examples: connects to PostgreSQL and makes the tables again. // ORM 1.0.0 needs PostgreSQL: it reads inserted rows back with RETURNING. // Before PHP 8.4 there is nothing to hide; from 8.4 on, ORM 1.0.0's optional // parameters raise deprecation notices. error_reporting(E_ALL & ~E_DEPRECATED); require __DIR__ . '/vendor/autoload.php'; $pdo = new PDO('pgsql:dbname=divengine_orm_examples'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->exec(<<<SQL SET client_min_messages = warning; DROP SCHEMA IF EXISTS shop CASCADE; CREATE SCHEMA shop; CREATE TABLE shop.person ( id serial PRIMARY KEY, first_name text NOT NULL, email text, created date NOT NULL DEFAULT '2026-01-01' ); SQL); divengine\orm::connectGlobal($pdo);
ORM 1.0.0 needs PostgreSQL. The examples, with the commands to run them again with ORM 1.0.0, are in the site's repository.