PHPackages                             nkizza/simplerelations - 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. nkizza/simplerelations

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

nkizza/simplerelations
======================

Simplerelations for Yii2.

1.0(9y ago)075BSD-3-ClausePHP

Since Mar 5Pushed 9y ago1 watchersCompare

[ Source](https://github.com/nkizza/simplerelations)[ Packagist](https://packagist.org/packages/nkizza/simplerelations)[ RSS](/packages/nkizza-simplerelations/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (0)

Simplerelations for Yii2
========================

[](#simplerelations-for-yii2)

Provides simple admin tool to manage relations. Configurate needed relation with RelatedBehavior, add RelatedWidget to your form and enjoy the easy relation management.

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
composer require nkizza/simplerelations:~1.0

```

or add

```
"nkizza/simplerelations" : "~1.0"
```

to the require section of your application's `composer.json` file.

Usage
-----

[](#usage)

This extension comes with two possibilities:

1. Behavior that serves to manage related active records. You have to provide a specific attribute for that records and configure validation rules first; then just add the behavior to your model.
2. Simple widget that serves to display your related records and provide functionality to add, edit and delete them. Widget is simply configurable and lets you create flexible forms for your related records.

**RelatedBehavior**

First, add the specific public attribute for your related records and include it into your model. Then, configure the behavior:

```
class Match extends \yii\db\ActiveRecord
{
    public $_players;

    public function behaviors() {
        return [
            [
                'class' => \nkizza\simplerelations\RelatedBehavior::className(),
                'attribute' => '_players', //attribute which stores the related data
                'uploadRelation' => 'players', //relation name
                'uploadModelScenario' => 'default', //you can provide the specific scenario name for the related models
                'fields' => [   //fields of related record that we manage using this behavior
                    'id_player', 'min_played', 'goals', 'assists', 'y_cards', 'r_cards',
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['_players'], 'safe'],
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getPlayers()
    {
        return $this->hasMany(PlayerMatch::className(), ['id_match' => 'id']);
    }

```

**RelationWidget**

Related widget helps to manage related records. It allows you to build flexible forms to edit related records within the current model form.

***Example of use***
Related widget extends `\yii\widgets\InputWidget`. There are two ways of using it, with an `ActiveForm` instance or as a widget setting up its `model` and `attribute`.

```

Players of match
