PHPackages                             phpfui/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. [Database &amp; ORM](/categories/database)
4. /
5. phpfui/orm

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

phpfui/orm
==========

A Fast, Lightweight and Minimal ORM for MySQL, SQLite, MariaDB and PostGre

V3.0.3(3mo ago)9289MITPHPPHP &gt;=8.1 &lt;8.6

Since Apr 4Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/phpfui/ORM)[ Packagist](https://packagist.org/packages/phpfui/orm)[ Docs](http://www.phpfui.com/?n=PHPFUI%5CORM)[ RSS](/packages/phpfui-orm/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (58)Used By (0)

PHPFUI\\ORM [![Tests](https://github.com/phpfui/ORM/actions/workflows/tests.yml/badge.svg)](https://github.com/phpfui/ORM/actions?query=workflow%3Atests) [![Latest Packagist release](https://camo.githubusercontent.com/871fa244891a1bbf44ed0a45f741c1911ab3fcda013827868c73b1c118d013db/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7068706675692f4f524d2e737667)](https://packagist.org/packages/phpfui/ORM) [![](https://camo.githubusercontent.com/742e8be8005b1fe76a64a5d8f5e6a5d4f63bf315e9a44a6d23e4e11c76b0555f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230362d627269676874677265656e2e7376673f7374796c653d666c6174)](https://camo.githubusercontent.com/742e8be8005b1fe76a64a5d8f5e6a5d4f63bf315e9a44a6d23e4e11c76b0555f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230362d627269676874677265656e2e7376673f7374796c653d666c6174)
================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#phpfuiorm---)

### PHPFUI\\ORM a minimal Object Relational Mapper (ORM) for MySQL, MariaDB, PostGre and SQLite3

[](#phpfuiorm-a-minimal-object-relational-mapper-orm-for-mysql-mariadb-postgre-and-sqlite3)

Why another PHP ORM? In writing minimal and fast websites, it was determined that existing PHP ORM solutions were overly complex. **PHPFUI\\ORM** is a little more than 7K lines of code in 52 files. It is designed to have a minimal memory footprint and excellent execution times for most database needs.

**PHPFUI\\ORM** demonstrates superior performance for both speed and memory usage verses other ORMs. This is proven by a [comparison of PHPFUI\\ORM to Other ORMs](https://github.com/phpfui/php-orm-sql-benchmarks) for different SQL implementations.

**PHPFUI\\ORM** is not an attempt to write an abstraction around SQL as other ORMs do, rather it is a way to work with SQL that closely matches the semantics of SQL, with the power of PHP objects. It allows PHP to manipulate SQL queries without having to write SQL in plain text. This is very useful for queries generated via user interfaces where the user is given a lot of flexability in how a query is defined.

Features
--------

[](#features)

- **Active Records** A fully type checked object interface and implement basic CRUD functionality.
- **Active Tables** Full table operations (select, update, insert and delete) including support for where, having, limits, ordering, grouping, joins and unions.
- **Data Cursors** Cursors implement **iterable** and **Countable** eliminating the need to read full arrays into memory.
- **Validation** Fully customizable and translatable backend validation.
- **Virtual Fields** Supports get and set semantics for any custom or calculated field such as Carbon dates.
- **Migrations** Simple migrations offer atomic up and down migrations.
- **Relations** Parent, children, one to one, many to many, and custom relationships.
- **Transactions** Object based transactions meaning exceptions will not leave an open transacton.
- **Type Safe** Prevents stupid type errors.
- **Injection Safe** Uses PDO placeholders and field sanitation to prevent injection attacks.
- **Raw SQL Query Support** Execute any valid SQL command.
- **Multiple Database Support** Work with multiple databases simultaneously.
- **Multi-Vendor Support** Built on PDO with support for MySQL, MariaDB, PostGre and SQLite.

Usage
-----

[](#usage)

### Setup

[](#setup)

```
$pdo = new \PHPFUI\ORM\PDOInstance($yourConnectionString);
// perform any custom configuration settings needed on $pdo
\PHPFUI\ORM::addConnection($pdo);
```

See some basic usage examples in [scripts/examples.php](https://github.com/phpfui/ORM/blob/main/scripts/examples.php)

### Active Record Example

[](#active-record-example)

```
$book = new \App\Record\Book();
$book->title = 'PHP ORM: The Right Way';
$book->price = 24.99;

$author = new \App\Record\Author();
$author->name = 'Bruce Wells';

$book->author = $author;  // Save the author
$book->save();            // Save the book
```

### Active Table Example

[](#active-table-example)

```
$bookTable = new \App\Table\Book();
$bookTable->setWhere(new \PHPFUI\ORM\Condition('title', '%orm%', new \PHPFUI\ORM\Operator\Like()));
$bookTable->join('author');

foreach ($bookTable->getDataObjectCursor() as $book)
  {
  echo "{$book->title} by {$book->name} is $ {$book->price}\n";
  }

// discount all PHP books to 19.99
$bookTableUpdater = new \App\Table\Book();
$bookTableUpdater->setWhere(new \PHPFUI\ORM\Condition('title', '%PHP%', new \PHPFUI\ORM\Operator\Like()));
$bookTableUpdater->update(['price' => 19.99]);

foreach ($bookTableUpdater->getRecordCursor() as $book)
  {
  echo "{$book->title} by {$book->author->name} is now $ {$book->price}\n";
  }
```

### Validation Example

[](#validation-example)

```
$book->title = 'This title is way to long for the database and will return a validation error. We should write a migration to make it varchar(255)!';
$errors = $book->validate();
foreach ($errors as $field => $fieldErrors)
  {
  echo "Field {$field} has the following errors:\n";
  foreach ($fieldErrors as $error)
    {
    echo $error . "\n";
    }
  }
```

### Migration Example

[](#migration-example)

Migrations are atomic and can be run in groups or individually up or down.

```
namespace App\Migration;

class Migration_1 extends \PHPFUI\ORM\Migration
  {

  public function description() : string
    {
    return 'Lengthen book.title field to 255';
    }

  public function up() : bool
    {
    return $this->alterColumn('book', 'title', 'varchar(255) not null');
    }

  public function down() : bool
    {
    return $this->alterColumn('book', 'title', 'varchar(50) not null');
    }
  }
```

Type Safety
-----------

[](#type-safety)

### Exceptions Supported

[](#exceptions-supported)

Exceptions are generated in the following conditions:

- Accessing field or offset that does not exist
- Deleting records without a where condition (can be overridden)
- Incorrect type for Operator (must be an array for **IN** for example)
- Passing an incorrect type as a primary key
- Invalid join type
- Joining on an invalid table

All of the above exceptions are programmer errors and strictly enforced. Empty queries are not considered errors. SQL may also return [Exceptions](https://www.php.net/manual/en/class.exception.php) if invalid fields are used.

### Type Conversions

[](#type-conversions)

If you set a field to the wrong type, the library logs a warning then converts the type via the appropriate PHP cast.

Multiple Database Support
-------------------------

[](#multiple-database-support)

While this is primarily a single database ORM, you can switch databases at run time. Save the value from `$connectionId = \PHPFUI\ORM::addConnection($pdo);` and then call `\PHPFUI\ORM::useConnection(connectionId);` to switch. `\PHPFUI\ORM::addConnection` will set the current connection.

The programmer must make sure the proper database is currently selected when database reads or writes happen and that any primary keys are correctly handled.

### Copy tables example:

[](#copy-tables-example)

```
// get the current connection to restore later
$currentConnection = \PHPFUI\ORM::getConnection();

$cursors = [];
// getRecordCursor will bind the cursor to the current database instance
$cursors[] = (new \App\Table\Author())->getRecordCursor();
$cursors[] = (new \App\Table\Book())->getRecordCursor();

// set up a new database connection
$pdo = new \PHPFUI\ORM\PDOInstance($newConnectionString);
$newConnectionId = \PHPFUI\ORM::addConnection($pdo);

foreach ($cursors as $cursor)
  {
  foreach ($cursor as $record)
    {
    $record->insert();	// insert into new database ($newConnectionId)
    }
  }
// back to the original database
\PHPFUI\ORM::useConnection($currentConnection);
```

Documentation
-------------

[](#documentation)

- [Setup](https://github.com/phpfui/ORM/blob/main/docs/1.%20Setup.md)
- [Active Record](https://github.com/phpfui/ORM/blob/main/docs/2.%20Active%20Record.md)
- [Active Table](https://github.com/phpfui/ORM/blob/main/docs/3.%20Active%20Table.md)
- [Cursors](https://github.com/phpfui/ORM/blob/main/docs/4.%20Cursors.md)
- [Virtual Fields](https://github.com/phpfui/ORM/blob/main/docs/5.%20Virtual%20Fields.md)
- [Migrations](https://github.com/phpfui/ORM/blob/main/docs/6.%20Migrations.md)
- [Validation](https://github.com/phpfui/ORM/blob/main/docs/7.%20Validation.md)
- [Translations](https://github.com/phpfui/ORM/blob/main/docs/8.%20Translations.md)
- [Transactions](https://github.com/phpfui/ORM/blob/main/docs/9.%20Transactions.md)
- [Miscellaneous](https://github.com/phpfui/ORM/blob/main/docs/10.%20Miscellaneous.md)

Full Class Documentation
------------------------

[](#full-class-documentation)

[PHPFUI/ORM](http://phpfui.com/?n=PHPFUI%5CORM)

License
-------

[](#license)

PHPFUI is distributed under the MIT License.

PHP Versions
------------

[](#php-versions)

This library only supports **modern** versions of PHP which still receive security updates. While we would love to support PHP from the late Ming Dynasty, the advantages of modern PHP versions far out weigh quaint notions of backward compatibility. Time to upgrade.

###  Health Score

50

—

FairBetter than 96% of packages

Maintenance80

Actively maintained with recent releases

Popularity19

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity76

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

Total

57

Last Release

113d ago

Major Versions

V1.7.13 → V2.0.02025-02-13

V2.0.09 → V3.0.02025-11-30

PHP version history (4 changes)V1.0PHP &gt;=8.1 &lt;8.3

V1.5.4PHP &gt;=8.1 &lt;8.4

V1.7.6PHP &gt;=8.1 &lt;8.5

V2.0.08PHP &gt;=8.1 &lt;8.6

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7434059?v=4)[Bruce Wells](/maintainers/PHPFUI)[@phpfui](https://github.com/phpfui)

---

Top Contributors

[![phpfui](https://avatars.githubusercontent.com/u/7434059?v=4)](https://github.com/phpfui "phpfui (145 commits)")

---

Tags

ormmysqlsqlitemariadbsqlmodelobjectmappingmapperfastlightweightactiverecordrelationalpostgre

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  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)[catfan/medoo

The lightweight PHP database framework to accelerate development

4.9k1.5M194](/packages/catfan-medoo)[tommyknocker/pdo-database-class

Framework-agnostic PHP database library with unified API for MySQL, MariaDB, PostgreSQL, SQLite, MSSQL, and Oracle. Query Builder, caching, sharding, window functions, CTEs, JSON, migrations, ActiveRecord, CLI tools, AI-powered analysis. Zero external dependencies.

845.7k](/packages/tommyknocker-pdo-database-class)[cycle/database

DBAL, schema introspection, migration and pagination

64690.9k31](/packages/cycle-database)[atlas/cli

Command-line interface for Atlas.

14124.6k10](/packages/atlas-cli)[ramadan/easy-model

A Laravel package for enjoyably managing database queries.

101.6k](/packages/ramadan-easy-model)

PHPackages © 2026

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