PHPackages                             bigdropinc/yii2-sti - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. bigdropinc/yii2-sti

ActiveYii2-extension[Database &amp; ORM](/categories/database)

bigdropinc/yii2-sti
===================

This extension provides realization of single table inheritance

1.0.4(8y ago)314.4kBSD-3-ClausePHP

Since May 7Pushed 8y ago6 watchersCompare

[ Source](https://github.com/bigdropinc/yii2-sti)[ Packagist](https://packagist.org/packages/bigdropinc/yii2-sti)[ RSS](/packages/bigdropinc-yii2-sti/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (6)Used By (0)

STI
===

[](#sti)

This extension provides realization of **single table inheritance**

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

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
-----

[](#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

[](#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

[](#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.

```
