thibaud-dauce/eloquent-inheritance-storage

此包已被废弃,不再维护。未建议替代包。

使用 Eloquent ORM 轻松管理继承

v0.1 2014-07-28 14:16 UTC

This package is auto-updated.

Last update: 2022-02-01 12:37:35 UTC


README

Build Status Software License

介绍

Eloquent Inheritance Storage 旨在扩展 Eloquent ORM 以支持扩展其他模型的模型。它允许您轻松存储和检索父模型和子模型。

此包是 单表继承 模式的扩展。它使用视图将来自类层次结构中多个表的数据组合在一起。通过这样做,我们避免了具有许多 NULL 值的表。

安装

需要 PHP 5.4+ 和 Laravel 4.2+。

要获取 Eloquent Inheritance Storage 的最新版本,只需在您的 composer.json 文件中包含 "thibaud-dauce/eloquent-inheritance-storage": "0.*"。然后,您需要运行 composer installcomposer update 来下载它并更新自动加载器。

一旦安装了 Eloquent Inheritance Storage,您需要注册服务提供程序。打开 app/config/app.php 并将以下内容添加到 providers 键中。

  • 'ThibaudDauce\EloquentInheritanceStorage\EloquentInheritanceStorageServiceProvider'

如果您愿意,可以在您的 app/config/app.php 文件的 aliases 键中注册 InheritanceStorage 门面。

  • 'InheritanceStorage' => 'ThibaudDauce\EloquentInheritanceStorage\Facades\InheritanceStorage'

模型配置示例

展示

让我们想象一下,我目前正在开发一款包含不同类型角色的视频游戏。我将有一些基本角色和一些特殊角色

  • 战士 将是一个具有 狂怒 属性的角色。
  • 法师 将是一个具有 魔法 属性的角色。

我的类层次结构将是以下内容

  • Character: id, name。
    • Warrior 继承自 Character: id, name, rage。
    • Wizard 继承自 Character: id, name, magic。

模型

ThibaudDauce\EloquentInheritanceStorage\ParentTrait 应用于 Character 模型(父类)。

<?php

use ThibaudDauce\EloquentInheritanceStorage\ParentTrait;

class Character extends Eloquent {

  use ParentTrait;

  protected $table = 'characters';
  protected $primaryKey = 'name';
}

WarriorWizard 模型(子类)不做任何操作。

<?php

class Warrior extends Character {

  protected $table = 'warriors';
}

class Wizard extends Character {

  protected $table = 'wizards';
}

数据库

我们将创建 3 个表和一个视图

  • 一个名为 characters_storage 的表,其中将只包含基本角色(来自父类)。
  • 一个名为 warriors 的表,其中将只包含战士(来自子类)。
  • 一个名为 wizards 的表,其中将只包含法师(来自子类)。
  • 一个名为 characters 的视图,其中将包含角色、战士和法师。

让我们创建我们的表。请注意不同的表名!

<?php

// Table for our parent class
Schema::create('characters_storage', function(Blueprint $table)
{
  $table->increments('id');
  $table->string('name')->unique();
});

// Tables for our child classes
Schema::create('warriors', function(Blueprint $table)
{
  $table->increments('id');
  $table->string('name')->unique();
  $table->integer('rage');
});

Schema::create('wizards', function(Blueprint $table)
{
  $table->increments('id');
  $table->string('name')->unique();
  $table->integer('magic');
});

最后,我们来创建一个包含我们的角色、战士和法师的 characters 视图。别忘了在你的视图中添加一个 class_name 字段。

<?php

DB::statement("
CREATE VIEW `characters` AS
  SELECT
    `characters_storage`.`id` AS `id` ,
    'Character' AS `class_name` ,
    `characters_storage`.`name` AS `name` ,
    NULL AS `rage` ,
    NULL AS `magic` ,
  FROM `characters_storage`
  UNION
  SELECT
    `warriors`.`id` AS `id` ,
    'Warrior' AS `class_name` ,
    `warriors`.`name` AS `name` ,
    `warriors`.`rage` AS `rage` ,
    NULL AS `magic` ,
  FROM `warriors`
  UNION
  SELECT
    `wizards`.`id` AS `id` ,
    'Wizard' AS `class_name` ,
    `wizards`.`name` AS `name` ,
    `wizards`.`magic` AS `magic` ,
    NULL AS `rage` ,
  FROM `wizards` ;
");

用法

获取模型

Character::all() 将返回包含 CharacterWarriorWizard 模型的集合。

Character::find($characterName) 将返回一个 Character

Character::find($warriorName) 将返回一个 Warrior

Warrior::find($warriorName) 将返回一个 Warrior

Warrior::find($characterName)Warrior::find($wizardName) 将抛出错误。

保存模型

Character::create(array('name' => 'Thibaud')) 将在 characters_storage 表中存储一个角色。

Warrior::create(array('name' => 'Thibaud', 'rage' => 10)) 将在 warriors 表中添加一行。

扩展包

存储名称

您可以通过在父模型中定义 $inheritanceStorageName 来使用不同的存储表名称。

示例:对于名为 characters 的视图和名为 characters-table 的存储表

<?php

use ThibaudDauce\EloquentInheritanceStorage\ParentTrait;

class Character extends Eloquent {

  use ParentTrait;

  protected $table = 'characters';
  protected $inheritanceStorageName = 'characters-table';
  protected $primaryKey = 'name';
}