norse-blue/parentity
Composer 安装命令:
composer require norse-blue/parentity
包简介
Parentity is a package that allows the use of MTI entities in Laravel 5.7+ using Eloquent models.
README 文档
README
Parentity is a package that allows the use of MTI (Multi-Table Inheritance) entities in Laravel 5.7+ using Eloquent models.
Installation
composer require "norse-blue/parentity"
Usage
-
Create parent model table with the following fields:
$table->string('entity_type')->nullable(); $table->unsignedInteger('entity_id')->nullable();
Note: Use the proper id type for your child models (unsignedInteger, unsignedBigInteger, ...).
-
Create parent model that extends
Modelsand usesIsMtiParentModeltrait:<?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', ]; }
Notes:
- The
$childTypeAliasesarray is optional. It allows to create child models using an alias instead of the full qualified class name. - The
$ownAttributesarray is optional. It allows the proxying of get and set calls between parent and child models (instead of$customer->entity->last_nameit allows to use$customer->last_name). It is recommended to specify this array so that everything works neatly.
- The
-
Create the child model(s) that extends
Modelsand usesIsMtiChildModeltrait:<?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', ]; }
Notes:
- All fields are mandatory so that the child model knows how to access the parent model.
Model creation
Creating a model from the parent class
To create a model from the parent class you need to specify the entity type before the attributes.
$customer = Customer::create(Person::class, [ 'name' => 'Axel', 'last_name' => 'Pardemann', ]);
Creating a model from the child class
The best way to create a model is from the child classes. Just include all the models (parent and child) attributes.
$person = Person::create([ 'name' => 'Axel', 'last_name' => 'Pardemann', ]);
In both cases a parent and a linked child will be created with the given attribute values, which will be stored in each model's table.
Model properties
There are two ways that we can access the model properties: explicitly or implicitly.
Explicit properties
When using explicit properties we explicitly ask for the parent or the entity property:
// 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;
Outputs:
Axel Pardemann
Axel Pardemann
Implicit properties
If set up correctly, instead of using the explicit property calls we can use the shorter version which implicitly proxies the call to the parent or child model:
// 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;
Outputs:
Axel Pardemann
Axel Pardemann
Code status and known issues
- This package is still a proof of concept. At the moment we can only create models and use the properties.
- It is planned to extend the functionality to the
make,updateandsavemethods first.
Contributing
Please see CONTRIBUTING for details.
norse-blue/parentity 适用场景与选型建议
norse-blue/parentity 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 78 次下载、GitHub Stars 达 4, 最近一次更新时间为 2018 年 09 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 norse-blue/parentity 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 norse-blue/parentity 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 78
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-09-23