norse-blue / parentity

Parentity 是一个允许在 Laravel 5.7+ 中使用 Eloquent 模型使用 MTI(多表继承)实体的包。

v0.1 2018-09-23 16:11 UTC

This package is auto-updated.

Last update: 2024-09-17 06:46:29 UTC


README

Parentity 是一个允许在 Laravel 5.7+ 中使用 Eloquent 模型使用 MTI(多表继承)实体的包。

安装

composer require "norse-blue/parentity"

使用方法

  1. 创建具有以下字段的父模型表

    $table->string('entity_type')->nullable();
    $table->unsignedInteger('entity_id')->nullable();

    注意:使用适合子模型的正确 id 类型(unsignedInteger, unsignedBigInteger, ...)。

  2. 创建扩展 Models 并使用 IsMtiParentModel 特性的父模型

    <?php
    
    namespace App;
    
    use App\Customers\Company;
    use App\Customers\Person;
    use Illuminate\Database\Eloquent\Model;
    use NorseBlue\Parentity\Traits\IsMtiParentModel;
    
    class Customer extends Model
    {
        use IsMtiParentModel;
    
        protected $fillable = [
            'name',
        ];
    
        /** @optional */
        protected $childTypeAliases = [
            'person' => Person::class,
            'company' => Company::class,
        ];
    
        /** @optional */
        protected $ownAttributes = [
            'id',
            'name',
            'entity_type',
            'entity_id',
        ];
    }

    注意事项

    • $childTypeAliases 数组是可选的。它允许使用别名而不是完全限定的类名来创建子模型。
    • $ownAttributes 数组是可选的。它允许在父模型和子模型之间代理获取和设置调用(而不是使用 $customer->entity->last_name,它允许使用 $customer->last_name)。建议指定此数组,以确保一切运行顺利。
  3. 创建扩展 Models 并使用 IsMtiChildModel 特性的子模型

    <?php
    
    namespace App;
    
    use App\Customer;
    use Illuminate\Database\Eloquent\Model;
    use NorseBlue\Parentity\Traits\IsMtiChildModel;
    
    class Person extends Model
    {
        use IsMtiChildModel;
        
        protected $parentModel = Customer::class;
    
        protected $parentEntity = 'entity';
    
        protected $fillable = [
            'last_name',
        ];
    }

    注意事项

    • 所有字段都是强制性的,这样子模型就知道如何访问父模型。

模型创建

从父类创建模型

要创建从父类创建的模型,需要在属性之前指定实体类型。

$customer = Customer::create(Person::class, [
    'name' => 'Axel',
    'last_name' => 'Pardemann',
]);

从子类创建模型

创建模型的最佳方式是从子类创建。只需包含所有模型(父类和子类)的属性即可。

$person = Person::create([
    'name' => 'Axel',
    'last_name' => 'Pardemann',
]);

在这两种情况下,都会创建一个父模型和一个关联的子模型,并使用给定的属性值,这些值将存储在每个模型的表中。

模型属性

我们可以通过两种方式访问模型属性:显式或隐式。

显式属性

当使用显式属性时,我们明确请求父属性或实体属性

// using the previously created models

// Explicit property from the parent model
echo $customer->name . " " . $customer->entity->last_name;

// Explicit property from the child model
echo $person->parent->name . " " . $person->last_name;

输出

Axel Pardemann
Axel Pardemann

隐式属性

如果设置正确,我们可以使用简短版本代替显式属性调用,该版本隐式代理调用到父或子模型

// using the previously created models

// Implicit property from the parent model
echo $customer->name . " " . $customer->last_name;

// Implicit property from the child model
echo $person->name . " " . $person->last_name;

输出

Axel Pardemann
Axel Pardemann

代码状态和已知问题

  • 此包仍然是概念验证。目前我们只能创建模型并使用属性。
  • 计划首先扩展功能到 makeupdatesave 方法。

贡献

有关详细信息,请参阅 CONTRIBUTING