承接 norse-blue/parentity 相关项目开发

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

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

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

  1. 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, ...).

  2. Create parent model that extends Models and uses IsMtiParentModeltrait:

    <?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 $childTypeAliases array is optional. It allows to create child models using an alias instead of the full qualified class name.
    • The $ownAttributes array is optional. It allows the proxying of get and set calls between parent and child models (instead of $customer->entity->last_name it allows to use $customer->last_name). It is recommended to specify this array so that everything works neatly.
  3. Create the child model(s) that extends Models and uses IsMtiChildModel trait:

    <?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, update and save methods 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 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 78
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-09-23