定制 bigdropinc/yii2-sti 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

bigdropinc/yii2-sti

最新稳定版本:1.0.4

Composer 安装命令:

composer require bigdropinc/yii2-sti

包简介

This extension provides realization of single table inheritance

README 文档

README

This extension provides realization of single table inheritance

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist bigdropinc/yii2-sti "*"

or add

"bigdropinc/yii2-sti": "*"

to the require section of your composer.json file.

Usage

You can implement STI into your models by following with two steps:

  • Add STI column (by default type) into database
  • Extend your model from bigdropinc\sti\ActiveRecord

More detailed usage can be found below.

Create a migration

Firstly to implement STI into database you should have STI column in database table. By default it named type, but you can choose whatever you want. Also you may want to add an index for this column. Your migration will looks like code below.

    public function safeUp()
    {
        $this->addColumn(User::tableName(), 'type', $this->string(20));
        $this->createIndex('idx-users-type', User::tableName(), 'type');
    }

    public function safeDown()
    {
        $this->dropColumn(User::tableName(), 'role');
    }

Active Record

Model witch should implement STI should extends from bigdropinc\sti\ActiveRecord. The better way is to create an base ActiveRecord class for whole your project and extend all models from it.

<?php 

namespace common\models;

class ActiveRecord extends \bigdropinc\sti\ActiveRecord
{

}

Now STI mechanism will automatically switch on if STI column was found. To disable this behavior you may override protected static function isStiEnabled() method. You may set default value of STI column by overriding protected static function getStiColumn()

By default STI column is a string and it's contain a class name. You may override protected static function getStiValue($className = null) to change this behavior

Finding models

For example you have some STI classes

class User extend bigdropinc\sti\ActiveRecord
{
}
class Client extend User
{
}
class Manager extend User
{
}

You can simply save any instance of this models and STI column will be automatically saved into database.

By calling User::find() your will get models of all types User, Client and Manager Each record will be populated into an appropriate class according it STI column (type)

By calling Client::find() or Manager::find() you will get only models of certain class.

ActiveQuery

Mechanism of finding records using the STI is implements by bigropinc\sti\ActiveQuery class. If you need to use your own ActiveQuery class with some logic with STI you should follow two simple steps:

  • Extend your ActiveQuery class from bigdropinc\sti\ActiveQuery
  • Override protected static function getActiveQuery() in model you need and return your ActiveQuery class name

Becomes

You can convert any model according STI scheme as in example below.


$user = User::findOne($id);
$client = $user->becomes($client::class);

Pay attention that after one object becomes to another it should not be used anymore.

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 6
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2017-05-07

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固