Download the PHP package jlorente/yii2-activerecord-inheritance without Composer
On this page you can find all versions of the php package jlorente/yii2-activerecord-inheritance. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jlorente/yii2-activerecord-inheritance
More information about jlorente/yii2-activerecord-inheritance
Files in jlorente/yii2-activerecord-inheritance
Package yii2-activerecord-inheritance
Short Description ActiveRecord Inheritance is an util to provide the Class Table Inheritance Pattern the to the Yii2 framework. It fakes inheritance between two ActiveRecord classes.
License MIT
Homepage https://github.com/jlorente/yii2-activerecord-inheritance
Informations about the package yii2-activerecord-inheritance
ActiveRecord Inheritance
ActiveRecord Inheritance is a util to provide the Class Table Inheritance Pattern to the Yii2 framework. Its motivation is to fake inheritance between two ActiveRecord classes.
Installation
Include the package as dependency under the bower.json file.
To install, either run
or add
to the section of your composer.json
file.
Usage
An example of usage could be:
Suppose you have the following schema.
To fake the inheritance between the two tables your code must look like this.
And you will be able to use Admin objects as if they were inheriting from User objects.
You can call parent methods and properties by using the parent relation property.
This trait is very useful for faking inheritance, however query filters should be applied on the parent relation.
Considerations
In order to use the trait properly, you must consider the following points
General
- By default, the primary key of the supposed child class is used as foreign key of the parent model, if you want to use another attribute as foreign key, yoy should overwrite the parentAttribute() method.
- The trait won't work with multiple primary and foreign keys.
ActiveRecord methods
- Methods overwriten from ActiveRecord in this trait like save, validate, etc... should not be overwriten on the class that uses the trait or functionality will be lost.
- To overwrite this methods properly, you could extend the class that uses the trait and overwrite there the methods, making sure that all methods call its parent implementation.
Inheritance call hierarchy
- The natural call hierarchy is preserved, so if a method or property exists on the called class or one of its ancestors, the method or property is called.
- If a method or property doesn't exist in the natural call hierarchy. The properly magic method is called (get, set, isset, unset, __call) and the yii2 call hierarchy is used. If the method or property isn't found in this call hierarchy the next step is executed.
- The previous process is repeteated for the faked parent object.
- The call hierarchy will stop when a method or property is found or when there are no more parents. In this case, an UnknownPropertyException or an UnknownMethodException will be raised.
- You can concatenate faked parent classes with the only limit of the php call stack.
License
Copyright © 2015 José Lorente Martín [email protected]. Licensed under the MIT license. See LICENSE.txt for details.