sam-it/yii2-singletableinheritance

Single Table Inheritance for Yii2

Maintainers

👁 SamMousa

Package info

github.com/SAM-IT/yii2-singletableinheritance

pkg:composer/sam-it/yii2-singletableinheritance

Statistics

Installs: 23 658

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 10

v2.0.1 2020-05-15 13:44 UTC

Requires

  • php: > 7.4

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT 4527b2ff8b561416ced3eb3e757dd784d522dbb9

  • Sam Mousa <sam.woop@mousa.nl>

README

👁 Scrutinizer Code Quality
👁 Code Coverage
👁 Continous integration
👁 Latest Stable Version
👁 Total Downloads
👁 License
👁 Monthly Downloads

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.