定制 nixphp/orm 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nixphp/orm

最新稳定版本:v0.1.0

Composer 安装命令:

composer require nixphp/orm

包简介

NixPHP ORM Plugin.

README 文档

README

Logo

NixPHP ORM Plugin

← Back to NixPHP

nixphp/orm

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

  • ✅ 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

composer require nixphp/orm

You also need nixphp/database for PDO access.

🛠 Configuration

This plugin uses the shared PDO instance from nixphp/database. Make sure your /app/config.php contains a working database section.

Example: MySQL

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

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

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

$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

$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

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

  • PHP >= 8.1
  • nixphp/framework >= 1.0
  • nixphp/database

📄 License

MIT License.

统计信息

  • 总下载量: 9
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-25