PHPackages                             nyx-solutions/yii2-nyx-ar-many-to-many-link - 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. [Database &amp; ORM](/categories/database)
4. /
5. nyx-solutions/yii2-nyx-ar-many-to-many-link

Abandoned → [https://github.com/jonatas-sas/yii2-m2m-behavior](/?search=https%3A%2F%2Fgithub.com%2Fjonatas-sas%2Fyii2-m2m-behavior)ArchivedYii2-extension[Database &amp; ORM](/categories/database)

nyx-solutions/yii2-nyx-ar-many-to-many-link
===========================================

Provides support for many-to-many relations saving in Yii2 ActiveRecord

5.0.0(3y ago)00BSD-3-ClausePHPPHP &gt;=8.1.0 &lt;8.2

Since Jun 3Pushed 3y ago1 watchersCompare

[ Source](https://github.com/nyx-solutions/yii2-nyx-ar-many-to-many-link)[ Packagist](https://packagist.org/packages/nyx-solutions/yii2-nyx-ar-many-to-many-link)[ RSS](/packages/nyx-solutions-yii2-nyx-ar-many-to-many-link/feed)WikiDiscussions main Synced 1mo ago

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

This extension provides support for ActiveRecord many-to-many relation saving. For example: single "item" may belong to several "groups", each group may be linked with several items. Unlike regular \[\[\\yii\\db\\BaseActiveRecord::link()\]\] usage, this extension automatically checks references existence, removes excluded references and provide support for web form composition.

For license information check the [LICENSE](LICENSE.md)-file.

[![Latest Stable Version](https://camo.githubusercontent.com/b4907282f72cc4b4fe2abb9f0a7063bfa174f205ef465bac23d7a62ecf74eea0/68747470733a2f2f706f7365722e707567782e6f72672f6e79782d736f6c7574696f6e732f796969322d6e79782d61722d6d616e792d746f2d6d616e792d6c696e6b2f762f737461626c65)](https://packagist.org/packages/nyx-solutions/yii2-nyx-ar-many-to-many-link)[![Total Downloads](https://camo.githubusercontent.com/edcb094726aafb8c12ccaa7375ee2241de341dbf4fb66d5c2fedf45a82ce4e1f/68747470733a2f2f706f7365722e707567782e6f72672f6e79782d736f6c7574696f6e732f796969322d6e79782d61722d6d616e792d746f2d6d616e792d6c696e6b2f646f776e6c6f616473)](https://packagist.org/packages/nyx-solutions/yii2-nyx-ar-many-to-many-link)[![Latest Unstable Version](https://camo.githubusercontent.com/0f20d3e3de3509655c26c7a6b975688d7e39046bf6320ad02be7b8359c24df16/68747470733a2f2f706f7365722e707567782e6f72672f6e79782d736f6c7574696f6e732f796969322d6e79782d61722d6d616e792d746f2d6d616e792d6c696e6b2f762f756e737461626c65)](https://packagist.org/packages/nyx-solutions/yii2-nyx-ar-many-to-many-link)[![License](https://camo.githubusercontent.com/dbbacfc1034494f691fbc2aca79577bed26d9af70ec7368e6bf3aa10406bba84/68747470733a2f2f706f7365722e707567782e6f72672f6e79782d736f6c7574696f6e732f796969322d6e79782d61722d6d616e792d746f2d6d616e792d6c696e6b2f6c6963656e7365)](https://packagist.org/packages/nyx-solutions/yii2-nyx-ar-many-to-many-link)[![Monthly Downloads](https://camo.githubusercontent.com/d961235d7f32bb1f5e817e6725d01fb63e27f8951e12366a6b03514339290c60/68747470733a2f2f706f7365722e707567782e6f72672f6e79782d736f6c7574696f6e732f796969322d6e79782d61722d6d616e792d746f2d6d616e792d6c696e6b2f642f6d6f6e74686c79)](https://packagist.org/packages/nyx-solutions/yii2-nyx-ar-many-to-many-link)[![Daily Downloads](https://camo.githubusercontent.com/6906574c6efc177478df4d616d921dafc213c6d1b317bed7c4106049e7f04bb7/68747470733a2f2f706f7365722e707567782e6f72672f6e79782d736f6c7574696f6e732f796969322d6e79782d61722d6d616e792d746f2d6d616e792d6c696e6b2f642f6461696c79)](https://packagist.org/packages/nyx-solutions/yii2-nyx-ar-many-to-many-link)[![composer.lock](https://camo.githubusercontent.com/c3efbf560a064e17bad7b97db83f72fed8268be45420945b648d78e6e0699924/68747470733a2f2f706f7365722e707567782e6f72672f6e79782d736f6c7574696f6e732f796969322d6e79782d61722d6d616e792d746f2d6d616e792d6c696e6b2f636f6d706f7365726c6f636b)](https://packagist.org/packages/nyx-solutions/yii2-nyx-ar-many-to-many-link)

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

[](#installation)

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

Either run

```
php composer.phar require --prefer-dist nyx-solutions/yii2-nyx-ar-many-to-many-link

```

or add

```
"nyx-solutions/yii2-nyx-ar-many-to-many-link": "*"

```

to the require section of your composer.json.

Usage
-----

[](#usage)

This extension provides support for ActiveRecord many-to-many relation saving. This support is granted via \[\[\\nyx\\db\\LinkManyToManyBehavior\]\] ActiveRecord behavior. You'll need to attach it to your ActiveRecord class and point the target "has-many" relation for it:

```
class Item extends ActiveRecord
{
    public function behaviors()
    {
        return [
            'linkGroupBehavior' => [
                'class' => LinkManyToManyBehavior::className(),
                'relation' => 'groups', // relation, which will be handled
                'relationReferenceAttribute' => 'groupIds', // virtual attribute, which is used for related records specification
            ],
        ];
    }

    public static function tableName()
    {
        return 'Item';
    }

    public function getGroups()
    {
        return $this->hasMany(Group::className(), ['id' => 'groupId'])->viaTable('ItemGroup', ['itemId' => 'id']);
    }
}
```

Being attached \[\[\\nyx\\db\\LinkManyToManyBehavior\]\] adds a virtual proprty to the owner ActiveRecord, which name is determined by \[\[\\nyx\\db\\LinkManyToManyBehavior::$relationReferenceAttribute\]\]. You will be able to specify related models primary keys via this attribute:

```
// Pick up related model primary keys:
$groupIds = Group::find()
    ->select('id')
    ->where(['isActive' => true])
    ->column();

$item = new Item();
$item->groupIds = $groupIds; // setup related models references
$item->save(); // after main model is saved referred related models are linked
```

The above example is equal to the following code:

```
$groups = Group::find()
    ->where(['isActive' => true])
    ->all();

$item = new Item();
$item->save();

foreach ($groups as $group) {
    $item->link('groups', $group);
}
```

> Attention: do NOT declare `relationReferenceAttribute` attribute in the owner ActiveRecord class. Make sure it does not conflict with any existing owner field or virtual property.

Virtual property declared via `relationReferenceAttribute` serves not only for saving. It also contains existing references for the relation:

```
$item = Item::findOne(15);
var_dump($item->groupIds); // outputs something like: array(2, 5, 11)
```

You may as well edit the references list for existing record, while saving linked records will be synchronized:

```
$item = Item::findOne(15);
$item->groupIds = array_merge($item->groupIds, [17, 21]);
$item->save(); // groups "17" and "21" will be added

$item->groupIds = [5];
$item->save(); // all groups except "5" will be removed
```

> Note: if attribute declared by `relationReferenceAttribute` is never invoked for reading or writing, it will not be processed on owner saving. Thus it will not affect pure owner saving.

Creating relation setup web interface
--------------------------------------

[](#creating-relation-setup-web-interface-)

The main purpose of \[\[\\nyx\\db\\LinkManyToManyBehavior::$relationReferenceAttribute\]\] is support for creating many-to-many setup web interface. All you need to do is declare a validation rule for this virtual property in your ActiveRecord, so its value can be collected from the request:

```
class Item extends ActiveRecord
{
    public function rules()
    {
        return [
            ['groupIds', 'safe'] // ensure 'groupIds' value can be collected on `populate()`
            // ...
        ];
    }

    // ...
}
```

Inside the view file you should use `relationReferenceAttribute` property as an attribute name for the form input:

```
