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

ActiveNixphp-plugin[Database &amp; ORM](/categories/database)

nixphp/orm
==========

NixPHP ORM Plugin.

v0.1.2(2mo ago)0101MITPHPPHP &gt;=8.3CI passing

Since Nov 30Pushed 2mo agoCompare

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

READMEChangelog (3)Dependencies (4)Versions (7)Used By (0)

[![Logo](https://camo.githubusercontent.com/075b2860e9651b98b8c190a8296595c54cff6900890d9e494f31131145e98a6f/68747470733a2f2f6e69787068702e6769746875622e696f2f646f63732f6173736574732f6e69787068702d6c6f676f2d736d616c6c2d7371756172652e706e67)](https://camo.githubusercontent.com/075b2860e9651b98b8c190a8296595c54cff6900890d9e494f31131145e98a6f/68747470733a2f2f6e69787068702e6769746875622e696f2f646f63732f6173736574732f6e69787068702d6c6f676f2d736d616c6c2d7371756172652e706e67)

[![NixPHP ORM Plugin](https://github.com/nixphp/orm/actions/workflows/php.yml/badge.svg)](https://github.com/nixphp/orm/actions/workflows/php.yml)

[← Back to NixPHP](https://github.com/nixphp/framework)

---

nixphp/orm
==========

[](#nixphporm)

> **Minimalistic object mapper for your NixPHP application.**

This plugin adds basic ORM support to NixPHP:
lightweight, readable, and ideal for small to medium use cases.

It supports nested entity saving (including pivot tables),
auto-discovery of related entities, and lazy-loading on read.

> 🧩 Part of the official NixPHP plugin collection.
> Use it if you want structured object handling – but without the complexity of full-stack ORM systems.

---

📦 Features
----------

[](#-features)

- ✅ Save any entity using `em()->save($entity)`
- ✅ Detects and stores relations automatically
- ✅ Supports `One-to-Many` and `Many-to-Many` out of the box
- ✅ Uses simple PHP classes, no annotations or metadata
- ✅ Includes lazy-loading via regular `getX()` methods
- ✅ Comes with a clean `AbstractRepository` for queries

---

📥 Installation
--------------

[](#-installation)

```
composer require nixphp/orm
```

You also need `nixphp/database` for PDO access.

---

🛠 Configuration
---------------

[](#-configuration)

This plugin uses the shared PDO instance from [`nixphp/database`](https://github.com/nixphp/database). Make sure your `/app/config.php` contains a working `database` section.

### Example: MySQL

[](#example-mysql)

```
return [
    // ...
    'database' => [
        'driver'   => 'mysql',
        'host'     => '127.0.0.1',
        'database' => 'myapp',
        'username' => 'root',
        'password' => '',
        'charset'  => 'utf8mb4',
    ]
];
```

### Example: SQLite

[](#example-sqlite)

```
return [
    // ...
    'database' => [
        'driver'   => 'sqlite',
        'database' => __DIR__ . '/../storage/database.sqlite',
    ]
];
```

Or for in-memory usage (great for testing):

```
return [
    // ...
    'database' => [
        'driver'   => 'sqlite',
        'database' => ':memory:',
    ]
];
```

---

🧩 Usage
-------

[](#-usage)

### Define your models

[](#define-your-models)

Models extend `AbstractModel` and use the `EntityTrait`.

```
class Product extends AbstractModel
{
    protected ?int $id = null;
    protected string $name = '';
    protected ?Category $category = null;
    protected array $tags = [];

    public function getTags(): array
    {
        if ($this->tags === []) {
            $this->tags = (new TagRepository())->findByPivot(Product::class, $this->id);
        }
        return $this->tags;
    }

    public function getCategory(): ?Category
    {
        if ($this->category === null && $this->category_id) {
            $this->category = (new CategoryRepository())->findOneBy('id', $this->category_id);
        }
        return $this->category;
    }
}
```

### Saving data

[](#saving-data)

```
$category = (new CategoryRepository())->findOrCreateByName('Books');
$tagA     = (new TagRepository())->findOrCreateByName('Bestseller');
$tagB     = (new TagRepository())->findOrCreateByName('Limited');

$product = new Product();
$product->name = 'NixPHP for Beginners';
$product->addCategory($category);
$product->addTag($tagA);
$product->addTag($tagB);

em()->save($product);
```

### Reading data

[](#reading-data)

```
$product = (new ProductRepository())->findOneBy('id', 1);

echo $product->name;
print_r($product->getCategory());
print_r($product->getTags());
```

Relations are lazy-loaded automatically when accessed.

---

📚 Philosophy
------------

[](#-philosophy)

This ORM is intentionally small and predictable. It provides just enough structure to manage entities and relations – without introducing complex abstractions or hidden behavior.

If you need validation, eager loading, event hooks, or advanced query building, you can integrate any larger ORM of your choice alongside it.

---

✅ Requirements
--------------

[](#-requirements)

- PHP &gt;= 8.1
- `nixphp/framework` &gt;= 1.0
- `nixphp/database`

---

📄 License
---------

[](#-license)

MIT License.

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance83

Actively maintained with recent releases

Popularity12

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity44

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

Total

3

Last Release

85d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f5d2bd3eecc9c3949d3caf98c4876c8e44c0c73fc80dbc65b55f4d91a5b63eae?d=identicon)[floknapp](/maintainers/floknapp)

---

Top Contributors

[![FloKnapp](https://avatars.githubusercontent.com/u/3774820?v=4)](https://github.com/FloKnapp "FloKnapp (19 commits)")

---

Tags

abstractiondatabasedatabase-abstractionnixphpormplugin

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  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)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90440.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)[wildside/userstamps

Laravel Userstamps provides an Eloquent trait which automatically maintains `created\_by` and `updated\_by` columns on your model, populated by the currently authenticated user in your application.

7511.7M13](/packages/wildside-userstamps)

PHPackages © 2026

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