2017 © Pedro Peláez
 

yii2-extension yii2-sti

This extension provides realization of single table inheritance

image

bigdropinc/yii2-sti

This extension provides realization of single table inheritance

  • Tuesday, June 27, 2017
  • by tv88dn
  • Repository
  • 6 Watchers
  • 4 Stars
  • 1,423 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 100 % Grown

The README.md

STI

This extension provides realization of single table inheritance, (*1)

Installation

The preferred way to install this extension is through composer., (*2)

Either run, (*3)

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

or add, (*4)

"bigdropinc/yii2-sti": "*"

to the require section of your composer.json file., (*5)

Usage

You can implement STI into your models by following with two steps:, (*6)

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

More detailed usage can be found below., (*7)

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., (*8)

    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., (*9)

``` <?php, (*10)

namespace common\models;, (*11)

class ActiveRecord extends \bigdropinc\sti\ActiveRecord {, (*12)

} Now STI mechanism will automatically switch on if STI column was found. To disable this behavior you may overrideprotected static function isStiEnabled()method. You may set default value of STI column by overridingprotected static function getStiColumn()```, (*13)

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, (*14)

Finding models

For example you have some STI classes, (*15)

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., (*16)

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), (*17)

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

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, (*19)

Becomes

You can convert any model according STI scheme as in example below., (*20)


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

Pay attention that after one object becomes to another it should not be used anymore., (*21)

The Versions

27/06 2017

dev-master

9999999-dev

This extension provides realization of single table inheritance

  Sources   Download

BSD-3-Clause

The Requires

 

by Vadim Trunov

yii2 table inheritance single sti

27/06 2017

1.0.4

1.0.4.0

This extension provides realization of single table inheritance

  Sources   Download

BSD-3-Clause

The Requires

 

by Vadim Trunov

yii2 table inheritance single sti

26/06 2017

1.0.3

1.0.3.0

This extension provides realization of single table inheritance

  Sources   Download

BSD-3-Clause

The Requires

 

by Vadim Trunov

yii2 table inheritance single sti

24/05 2017

1.0.2

1.0.2.0

This extension provides realization of single table inheritance

  Sources   Download

BSD-3-Clause

The Requires

 

by Vadim Trunov

yii2 table inheritance single sti

18/05 2017

v1.0.1

1.0.1.0

This extension provides realization of single table inheritance

  Sources   Download

BSD-3-Clause

The Requires

 

by Vadim Trunov

yii2 table inheritance single sti

07/05 2017

1.0

1.0.0.0

This extension provides realization of single table inheritance

  Sources   Download

BSD-3-Clause

The Requires

 

by Vadim Trunov

yii2 table inheritance single sti