PHPackages                             siddthartha/yii2-relational-set - 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. siddthartha/yii2-relational-set

ActiveLibrary

siddthartha/yii2-relational-set
===============================

Represents Yii2 m2m junction relation as PK-array field

06PHP

Since Apr 2Pushed 9y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (2)Used By (0)

yii2-relational-set
===================

[](#yii2-relational-set)

Represents Yii2 m2m junction relation as array field. Stores it's changes as difference without cleaning relation.

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

[](#installation)

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

Either run

```
$ composer require siddthartha/yii2-relational-set
```

or add

```
"siddthartha/yii2-relational-set": "*"

```

to the `require` section of your `composer.json` file.

Usage
-----

[](#usage)

### Host model

[](#host-model)

```
class Host extends \yii\db\ActiveRecord
{
    public $_slaves;

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'sets' => [
                'class' => \siddthartha\behaviors\RelationalSetBehavior::class,
                'attributes' => [
                    '_slaves' => 'slaves',
                ],
            ],
        ];
    }

    /**
     * @return \yii\db\ActiveQueryInterface
     */
    public function getSlaves()
    {
        return $this->hasMany(Slave::class, ['id' => 'id_slave'])
            ->viaTable(HostSlave::tableName(), ['id_host' => 'id'])
            ->indexBy('id');
    }
}
```

### View code example

[](#view-code-example)

```
