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.
- β
Save any entity using
em()->save($entity) - β Detects and stores relations automatically
- β
Supports
One-to-ManyandMany-to-Manyout of the box - β Uses simple PHP classes, no annotations or metadata
- β
Includes lazy-loading via regular
getX()methods - β
Comes with a clean
AbstractRepositoryfor queries
composer require nixphp/ormYou also need nixphp/database for PDO access.
This plugin uses the shared PDO instance from nixphp/database.
Make sure your /app/config.php contains a working database section.
return [
// ...
'database' => [
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'myapp',
'username' => 'root',
'password' => '',
'charset' => 'utf8mb4',
]
];return [
// ...
'database' => [
'driver' => 'sqlite',
'database' => __DIR__ . '/../storage/database.sqlite',
]
];Or for in-memory usage (great for testing):
return [
// ...
'database' => [
'driver' => 'sqlite',
'database' => ':memory:',
]
];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;
}
}$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);$product = (new ProductRepository())->findOneBy('id', 1);
echo $product->name;
print_r($product->getCategory());
print_r($product->getTags());Relations are lazy-loaded automatically when accessed.
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.
- PHP >= 8.1
nixphp/framework>= 1.0nixphp/database
MIT License.
