proai/eloquent-inheritance 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

proai/eloquent-inheritance

Composer 安装命令:

composer require proai/eloquent-inheritance

包简介

Single table inheritance for Laravel inspired by the Rails implementation

README 文档

README

Latest Stable Version Total Downloads Latest Unstable Version License

Single table inheritance is a way to emulate object-oriented inheritance in a relational database. While other frameworks like Ruby on Rails have a built-in implementation for this pattern, Laravel has not. This package aims to provide an as easy as possible implementation for Laravel.

Installation

You can install the package via composer:

composer require proai/eloquent-inheritance

Please note that you need at least PHP 8.0 and Laravel 9 for this package.

Usage

First you need to add the Inheritance trait to your root model:

use ProAI\Inheritance\Inheritance;

class Pet extends Model
{
    use Inheritance;

    //
}

Then you can extend the root model by other models that use the same table:

class Cat extends Pet
{
    //
}

class Dog extends Pet
{
    //
}

Note that you need a table pets with a column called type in order to make the example above work.

Whenever a Cat or Dog model is instantiated, the attribute type will be set to the classname of the class (e.g. App\Models\Cat).

Other than that there is no magic and a Cat or a Dog model will behave just like a normal Eloquent model. You can define cat or dog specific attributes and relationships on these models. An attribute only set for dogs for example should be a nullable column on the table, so that it is set for dogs but null for other pets.

Root model behaviour

You can query the root model like any other model. However, the returned models will be transformed based by the type name. For example if there is one cat and one dog in the database, Pet::all(); will return one Dog and one Cat model.

It is not possible to use new Pet($attributes); when you specify the type in $attributes. Please use the static method Pet::new($attributes) instead. This method returns a new model based on the given type. For example if the type in $attributes is App\Models\Cat, it will return an instance of App\Models\Cat. If no type is specified, an instance of App\Models\Pet will be returned.

Custom type names

If you want to use different names for the type column than the classname, you can use the static $inheritanceMap property:

class Pet extends Model
{
    use Inheritance;

    protected static $inheritanceMap = [
        'cat' => Cat::class,
        'dog' => Dog::class,
    ];
}

Custom type column

By default the name of the type column is type. However, you can set a custom type column name:

use ProAI\Inheritance\Inheritance;

class Pet extends Model
{
    use Inheritance;

    protected static $inheritanceColumn = 'pet_type';
}

Support

Bugs and feature requests are tracked on GitHub.

License

This package is released under the MIT License.

proai/eloquent-inheritance 适用场景与选型建议

proai/eloquent-inheritance 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.03k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 06 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「orm」 「laravel」 「inheritance」 「table」 「eloquent」 「single」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 proai/eloquent-inheritance 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 proai/eloquent-inheritance 我们能提供哪些服务?
定制开发 / 二次开发

基于 proai/eloquent-inheritance 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-06-04