PHPackages                             udokmeci/yii2-closest-relation-trait - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. udokmeci/yii2-closest-relation-trait

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

udokmeci/yii2-closest-relation-trait
====================================

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

12572[1 issues](https://github.com/udokmeci/yii2-closest-relation-trait/issues)PHP

Since Apr 13Pushed 11y ago2 watchersCompare

[ Source](https://github.com/udokmeci/yii2-closest-relation-trait)[ Packagist](https://packagist.org/packages/udokmeci/yii2-closest-relation-trait)[ RSS](/packages/udokmeci-yii2-closest-relation-trait/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependenciesVersions (1)Used By (0)

yii2-closest-relation-trait
===========================

[](#yii2-closest-relation-trait)

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

Why?
====

[](#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.

And if you are using `yii2` with more than one app you have to move relations as you extend models.

This trait (yes I have used trait instead of behaviours) add capability to call nearest Class sharing namespace of the model and its parents.

For example, let's say we are using `yii2-advance-template` so we may have 3 or more childs of same class.

call the class `Page` and relation to `Author`

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.

How to use?
===========

[](#how-to-use)

\##Installation with Composer Just add the line under `require` object in your `composer.json` file.

```
{
  "require": {
    "udokmeci/yii2-closest-relation-trait" : "dev-master"
  }
}
```

then run

```
$> composer update
```

\##Add trait to your base model. ###Via Model Generator Add following `generators` array to your configuration file.

```
$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:

```
