承接 dive-be/eloquent-super 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

dive-be/eloquent-super

Composer 安装命令:

composer require dive-be/eloquent-super

包简介

Lightweight MTI (Multi-Table Inheritance) support for Eloquent models

README 文档

README

Lightweight MTI (Multiple Table Inheritance) support for Eloquent models.

Latest Version on Packagist Software License Total Downloads

What is "Multiple Table Inheritance" exactly?

MTI allows one to have separate database tables for each "sub class" that shares a common "super class".

"Can't I just define a type column in my table and call it a day?"

Well, it depends. If the only thing you'd like to do is adding specific behavior to each sub type (user - admin for example), then Single Table Inheritance is definitely the better choice here.

However, if the sub types have very different data fields, then MTI is the better tool. Using STI in this case will cause the table in question to have a lot of NULL columns.

What problem does this package solve?

Short answer

As a matter of fact, it solves absolutely nothing. "Why this package, then?" you may ask. Well, read on.

Long answer

You see, Eloquent already gives us the ability to define polymorphic relationships. The only thing you need to start leveraging MTI capabilities in Eloquent is a MorphOne relationship. This package adds a nice DX layer on top of the existing functionality, so it is a tad nicer to work with these kind of tightly coupled relationships.

So, the "meat and potatoes" of this package is delegating calls to the defined super relationship (and a couple more things). There is no real "parent" class in an object oriented sense. It is a conscious decision to not sprinkle too much magic on the models.

Installation

composer require dive-be/eloquent-super

Usage

Super / parent class 👱🏻‍♂️

Migrations

The super model should define a morphs relationship that follows Laravel's naming conventions: the model's singular name in snake case + able.

Schema::create('addresses', static function (Blueprint $table) {
    $table->id();
    $table->foreignId('country_id')->constrained();
    $table->morphs('addressable'); // ==> mandatory
    
    // ... other columns
});

Class definition

The super class must define a fillable array in order to determine which attributes belong to which database tables. Without it, there is no way to distinguish the super's columns from the sub's columns.

class Address extends Model
{
    protected $fillable = ['city', 'country_id', 'street', 'postal_code'];

    public function country(): BelongsTo
    {
        return $this->belongsTo(Country::class);
    }
}

Sub / child classes 👶🏼

It is not mandatory for the sub classes to define a fillable array. Setting $guarded to an empty array is perfectly fine as well.

Class definition

class ShippingAddress extends Model
{
    use \Dive\EloquentSuper\InheritsFromSuper;

    protected $fillable = ['email', 'phone', 'contact_person', 'is_expedited', 'courier'];
    
    protected function getSuperClass(): string
    {
        return Address::class;
    }
}
class InvoiceAddress extends Model
{
    use \Dive\EloquentSuper\InheritsFromSuper;

    protected $fillable = ['company_id', 'email', 'fax', 'phone', 'language'];
    
    protected function getSuperClass(): string
    {
        return Address::class;
    }
}

Capabilities 💪

Partitioning of data when saving a sub model

$address = ShippingAddress::create($request->validated());

$address->getAttributes(); // 'email', 'phone', 'contact_person', 'is_expedited', 'courier'
$address->super->getAttributes(); // 'city', 'country_id', 'street', 'postal_code'

Attribute / relationship retrieval from super model

$address->city; // Ghent
$address->super->city; // Ghent

$address->country; // App\Models\Country { #2981 }
$address->super->country; // App\Models\Country { #2981 }

Deleting the super along with the sub model

$address->delete(); // Database transaction in the background

Note: only the sub model will be trashed if both the super and sub use the "SoftDeletes" trait

A note on always eager loading the "super" relationship 📣

It does not make sense for the sub model to exist without its complementary data from the super model. By having two tables, we are able to achieve a normalized database, but in code, it only makes sense when they coexist as a whole.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email oss@dive.be instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

dive-be/eloquent-super 适用场景与选型建议

dive-be/eloquent-super 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.42k 次下载、GitHub Stars 达 19, 最近一次更新时间为 2021 年 02 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-02-19