srlabs/eloquent-sti 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

srlabs/eloquent-sti

最新稳定版本:v6.0.0

Composer 安装命令:

composer require srlabs/eloquent-sti

包简介

Implement Single Table Inheritance within the Eloquent ORM

README 文档

README

This package provides an easy way to extend Eloquent model objects to provide support for single table inheritance. Inspired by an article written by Pallav Kaushish.

Installation

This package should be installed via composer:

$ composer require srlabs/eloquent-sti

Usage

In your Model, add the SingleTableInheritanceTrait trait and then specify these configuration values:

  • table: The name of the database table assigned to the base model
  • morphClass: The full class name of the base Eloquent Object
  • discriminatorColumn: The column in the database table used to distinguish STI entity types
  • inheritanceMap: An array mapping discriminator column values to corresponding entity classes

Here is an imaginary Widget model as an example:

// app/Epiphyte/Widget.php
class Widget extends Illuminate\Database\Eloquent\Model {

    /*****************************************************************************
     *  Eloquent Configuration
     *****************************************************************************/

    protected $guarded = ['id'];
    protected $fillable = ['name', 'description', 'status'];

    /*****************************************************************************
     * Single Table Inheritance Configuration
     *****************************************************************************/

    use SingleTableInheritanceTrait;
    protected $table = 'widgets';
    protected $morphClass = 'Epiphyte\Widget';
    protected $discriminatorColumn = 'status';
    protected $inheritanceMap = [
        'new' => 'Epiphyte\Entities\Widgets\NewWidget',
        'processed' => 'Epiphyte\Entities\Widgets\ProcessedWidget'
        'complete' => 'Epiphyte\Entities\Widgets\CompleteWidget'
    ];

    // ...
}

Next you need to create each of your child entity classes. I often keep them in an Entities folder, but any namespaced location will work.

Here is a hypothetical example:

// app/Epiphyte/Entities/Widgets/NewWidget.php
class NewWidget extends Epiphyte\Widget {

    /**
     * Limit the query scope if we define a query against the base table using this class.
     *
     * @param bool $excludeDeleted
     *
     * @return $this
     */
    public function newQuery($excludeDeleted = true)
    {
        return parent::newQuery($excludeDeleted)->where('status', '=', 'new');
    }

    // Remaining child entity methods go here...
}

Whenever Eloquent wants to return an instance of the base model it will use the value of the discriminator column to determine the appropriate entity type and return an instance of that class instead. This holds true for all Eloquent actions but it will not work on direct database (i.e. DB::table()) calls.

Providing the newQuery() method in the child class will allow you to use the Entity as a traditional Eloquent accessor that only returns entities of its own type. In this case, NewWidget::all(); would return all of the widgets flagged as 'new' in the database.

Any questions about this package should be posted on the package website.

统计信息

  • 总下载量: 3.78k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-01-19

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固