PHPackages                             sam-it/yii2-singletableinheritance - 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. [Framework](/categories/framework)
4. /
5. sam-it/yii2-singletableinheritance

ActiveLibrary[Framework](/categories/framework)

sam-it/yii2-singletableinheritance
==================================

Single Table Inheritance for Yii2

v2.0.1(6y ago)023.7k[10 PRs](https://github.com/SAM-IT/yii2-singletableinheritance/pulls)MITPHPPHP &gt; 7.4

Since May 6Pushed 4y ago1 watchersCompare

[ Source](https://github.com/SAM-IT/yii2-singletableinheritance)[ Packagist](https://packagist.org/packages/sam-it/yii2-singletableinheritance)[ RSS](/packages/sam-it-yii2-singletableinheritance/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (4)Dependencies (3)Versions (15)Used By (0)

yii2-singletableinheritance
===========================

[](#yii2-singletableinheritance)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/a46f4e1f03c1ab3bb7b30db035d5b69585601920d9e08dfc876d65026dd23f85/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f53414d2d49542f796969322d73696e676c657461626c65696e6865726974616e63652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/SAM-IT/yii2-singletableinheritance/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/352e7abceb1e88d716adfa2920b774649f3c4fb14514652baf259d77b55009aa/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f53414d2d49542f796969322d73696e676c657461626c65696e6865726974616e63652f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/SAM-IT/yii2-singletableinheritance/?branch=master)[![Continous integration](https://github.com/SAM-IT/yii2-singletableinheritance/workflows/Continous%20integration/badge.svg)](https://github.com/SAM-IT/yii2-singletableinheritance/workflows/Continous%20integration/badge.svg)[![Latest Stable Version](https://camo.githubusercontent.com/1e4fc8554d524e8b9c320be2087f8a0ddeea4d1af8f1ba8a37d3377d5f517e7e/68747470733a2f2f706f7365722e707567782e6f72672f73616d2d69742f796969322d73696e676c657461626c65696e6865726974616e63652f762f737461626c65)](https://packagist.org/packages/sam-it/yii2-singletableinheritance)[![Total Downloads](https://camo.githubusercontent.com/822fd1aac7fa649e1ef9bed458a9fd3e70d8903e103e6d53033a654f7f6d8e86/68747470733a2f2f706f7365722e707567782e6f72672f73616d2d69742f796969322d73696e676c657461626c65696e6865726974616e63652f646f776e6c6f616473)](https://packagist.org/packages/sam-it/yii2-singletableinheritance)[![License](https://camo.githubusercontent.com/bfe5b6cbeaa010fbae784343dec929a1cef7ad3e8321d4ad6c58c436cfe74744/68747470733a2f2f706f7365722e707567782e6f72672f73616d2d69742f796969322d73696e676c657461626c65696e6865726974616e63652f6c6963656e7365)](https://packagist.org/packages/sam-it/yii2-singletableinheritance)[![Monthly Downloads](https://camo.githubusercontent.com/3a77ad28789d9f9115ede3087e821131669e19fb1ce29569aeac5314107da5db/68747470733a2f2f706f7365722e707567782e6f72672f73616d2d69742f796969322d73696e676c657461626c65696e6865726974616e63652f642f6d6f6e74686c79)](https://packagist.org/packages/sam-it/yii2-singletableinheritance)

Yii2 Single Table Inheritance (STI) using traits

The reason this is implemented as traits is to prevent developers from putting 3rd party classes in their class hierarchy. This implementation uses 2 traits, 1 for the query class and 1 for the model class.

The query trait adds a filter based on the model class configured for the query. It implements the `prepare()` function. In case you use this trait on a class that also implements the `prepare()` function you MUST manually call ours like this:

```
class MyQuery extends \yii\db\ActiveQuery
{
    use \SamIT\Yii2\SingleTableInheritance\SingleTableInheritanceQueryTrait;

    public function prepare($builder)
    {
        // Your own code here
        $this->prepareSingleTableInheritance();
        // Your own code here
        return parent::prepare($builder);
    }
}
```

The model trait is a bit more complicated but still has a small surface of interaction with your code. It uses the `init()`function, so if you override that in the base STI class (the one where you use the trait) you must call our init function:

```
class Transport extends \yii\db\ActiveRecord
{
    use \SamIT\Yii2\SingleTableInheritance\SingleTableInheritanceTrait;
    public function init(): void
    {
        self::initSingleTableInheritance($this);
    }
}
```

Configure your inheritance config by implementing a static function:

```
class Transport extends \yii\db\ActiveRecord
{
    use \SamIT\Yii2\SingleTableInheritance\SingleTableInheritanceTrait;
    private static function inheritanceConfig(): array
    {
        return [
            'column' => 'type',
            'map' => [
                Car::class => 'car',
                Bike::class => 'bike'
            ]
        ];
    }
}
```

This function is only called once and its results are cached.

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 89.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

4

Last Release

2241d ago

Major Versions

v1.0.1 → v2.0.02020-05-13

PHP version history (2 changes)v1.0.0PHP &gt; 7.3

v2.0.0PHP &gt; 7.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/18b13c534e3812b66a72645fe215301b54fc4d288f6396fee9385b681e27da18?d=identicon)[SamMousa](/maintainers/SamMousa)

---

Top Contributors

[![SamMousa](https://avatars.githubusercontent.com/u/547021?v=4)](https://github.com/SamMousa "SamMousa (25 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (3 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/sam-it-yii2-singletableinheritance/health.svg)

```
[![Health](https://phpackages.com/badges/sam-it-yii2-singletableinheritance/health.svg)](https://phpackages.com/packages/sam-it-yii2-singletableinheritance)
```

###  Alternatives

[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k39.6M299](/packages/laravel-dusk)[nineinchnick/edatatables

Grid widget for the Yii Framework, wrapper for the DataTables jQuery plugin

173.2k](/packages/nineinchnick-edatatables)[link-cloud/fast-hyperf

LinkCloud Fast Hyperf

241.2k1](/packages/link-cloud-fast-hyperf)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
