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 override
protected static function isStiEnabled()method.
You may set default value of STI column by overriding
protected 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)