empu/eloquent-subtype

在这里输入您的包描述

v0.1.0 2022-09-06 01:09 UTC

This package is auto-updated.

Last update: 2024-09-26 11:41:22 UTC


README

超类和子类

有时,数据模型中的少数实体可能除了具有一个或多个独特属性外,还可能在其自身内部共享一些公共属性(属性)。基于这些属性,这些实体被分类为超类和子类实体。

超类 是一个实体类型,它与一个或多个子类有关系(父子关系),并且包含与其子类共有的属性。子类 是超类实体的子组,具有独特的属性,但它们将不同于每个子类。

超类和子类是父类和子类实体,超类和子类的主键始终相同。

案例

                    +---------------+
                    |    Parties    |
                    +---------------+
                    | ssn           |
                    | name          |
                    | email         |
                    +---------------+
                           |
                           O
                           |
            --------------------------------------
           |                                      |
    +--------------+                    +------------------+
    |   Teachers   |                    |     Students     |
    +--------------+                    +------------------+
    | salary       |                    | major_department |
    | date_of_hire |                    +------------------+
    +--------------+

在这里,“Parties”是超类或父实体,而子实体“Employees”和“Students”是子类。

如何安装

将包添加到项目中

这是一个有效的 composer 包,但尚未在 packagist 仓库中注册。

要安装此包,请将 git@gitlab.com:empu/eloquent-subtype.git 添加为仓库 URL,并将 empu/eloquent-subtype 添加到您的 composer.json 中的项目依赖项。

{
    "name": "laravel/laravel",
    "type": "project",
    "repositories" : [
        {
            "type": "vcs",
            "url": "git@gitlab.com:empu/eloquent-subtype.git"
        }
    ],
    // ... other settings
}

从您的项目根目录的终端运行 composer install empu/eloquent-subtype:dev-master

准备表和模型

为了使 eloquent-subtype 运行良好,实体表和模型必须遵循一些规则。

超类实体

parties 超类表的架构

    Schema::create('parties', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('ssn')->unique();
        $table->string('name');
        $table->string('email')->unique()->nullable();
        $table->timestamps();
    });

parties 模型

use Empu\EloquentSubtype\Contracts\InheritableEntity;
use Illuminate\Database\Eloquent\Model;

class Party extends Model implements InheritableEntity
{
    public function descendibleColumns(): array
    {
        return ['ssn', 'name', 'email'];
    }
}

子类实体

students 子类表的架构

    Schema::create('students', function (Blueprint $table) {
        $table->foreignId('super_id')->constrained('parties');
        $table->primary('super_id');
        $table->string('major_department');
        $table->timestamps();
    });

students 模型

use Empu\EloquentSubtype\Concerns\HasSupertype;
use Empu\EloquentSubtype\Contracts\InteractWithSupertype;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Student extends Model implements InteractWithSupertype
{
    use HasSupertype;

    protected $supertype = 'party';

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

子类应用实例

$student = App\Models\Student::make();
$student->ssn = 112233;
$student->name = 'Aqlan';
$student->email = 'aqlan@example.com';
$student->major_department = 'Tarbia';
$student->save();

dump(App\Models\Student::all());
// Illuminate\Database\Eloquent\Collection {#3925
//     all: [
//         App\Models\Student {#4285
//             super_id: 2,
//             major_department: "Tarbia",
//             created_at: "2022-09-06 00:47:07",
//             updated_at: "2022-09-06 00:47:07",
//             id: 2,
//             ssn: "112233",
//             name: "Aqlan",
//             email: "aqlan@example.com",
//         },
//     ],
// }
dump(App\Models\Party::all());
// Illuminate\Database\Eloquent\Collection {#4534
//     all: [
//         App\Models\Party {#4540
//             id: 2,
//             ssn: "112233",
//             name: "Aqlan",
//             email: "aqlan@example.com",
//             created_at: "2022-09-06 00:47:07",
//             updated_at: "2022-09-06 00:47:07",
//         },
//     ],
// }

待办事项

[] 从现有超类创建子类实体 [] 命令以生成适当的迁移 [] README 需要整理,编写文档很困难