yii2-closest-relation-trait
This trait adds capability to fetch nearest related model class without need of redefininf relations on extensions., (*1)
Why?
I like using gii
however if you have frequently changed table like me then it is a pain to continue using gii
or adding changes by hand., (*2)
And if you are using yii2
with more than one app you have to move relations as you extend models., (*3)
This trait (yes I have used trait instead of behaviours) add capability to call nearest Class sharing namespace of the model and its parents., (*4)
For example, let's say we are using yii2-advance-template
so we may have 3 or more childs of same class., (*5)
call the class Page
and relation to Author
, (*6)
you probably has it in common\models
, frontend\models
and backend\models
so if you don't override the relations you probably get common\models\Author
with getter. It is easy to copy and paste relations however adding a trait to your base model is easier., (*7)
How to use?
Installation with Composer
Just add the line under require
object in your composer.json
file.
``` json
{
"require": {
"udokmeci/yii2-closest-relation-trait" : "dev-master"
}
}, (*8)
then run
``` console
$> composer update
Add trait to your base model.
Via Model Generator
Add following generators
array to your configuration file., (*9)
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
'generators' => [
'model'=>'udokmeci\yii2closestrelation\model\Generator'
],
];
Manual
Just add use
statement in class and pass the all relation className
params from static::getRelationName()
method.
Example below:, (*10)
<?php
namespace common\models\base;
use Yii;
class Page extends \yii\db\ActiveRecord
{
//here is the trait and see also the relation
use \udokmeci\yii2closestrelation\ClosestRelationTrait
/**
* @inheritdoc
*/
public static function tableName()
{
return 'page';
}
/**
* @return \yii\db\ActiveQuery
*/
public function getAuthor()
{
return $this->hasOne(static::getRelationName(Author::className()), ['author_id' => 'id']);
}
}
Now if you have both \frontend\models\Page
and \frontend\models\Author
relation returns \frontend\models\Author
if you dont have both then \common\models\base\Author
is returned again., (*11)
Enjoy it., (*12)