2017 © Pedro Peláez
 

library yii2-closest-relation-trait

This trait adds capability to fetch nearest related model class without need of redefininf relations on extensions.

image

udokmeci/yii2-closest-relation-trait

This trait adds capability to fetch nearest related model class without need of redefininf relations on extensions.

  • Monday, April 13, 2015
  • by udokmeci
  • Repository
  • 2 Watchers
  • 2 Stars
  • 146 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

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)

The Versions

13/04 2015

dev-master

9999999-dev https://github.com/udokmeci

This trait adds capability to fetch nearest related model class without need of redefininf relations on extensions.

  Sources   Download

MIT

The Requires

 

yii2 relation closest