PHPackages                             voskobovich/yii2-many-many-behavior - 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. voskobovich/yii2-many-many-behavior

ActiveYii2-behavior[Database &amp; ORM](/categories/database)

voskobovich/yii2-many-many-behavior
===================================

This behavior makes it easy to maintain relations many-to-many in the your ActiveRecord model.

v3.2.2(10y ago)110188.4k↑128.9%3MITPHP &gt;=5.4.0

Since Nov 30Compare

[ Source](https://github.com/voskobovich/yii2-many-to-many-behavior)[ Packagist](https://packagist.org/packages/voskobovich/yii2-many-many-behavior)[ Docs](https://github.com/voskobovich/ManyToManyBehavior)[ RSS](/packages/voskobovich-yii2-many-many-behavior/feed)WikiDiscussions Synced 1w ago

READMEChangelog (4)Dependencies (2)Versions (12)Used By (3)

Yii2 ManyToMany Behavior
========================

[](#yii2-manytomany-behavior)

This behavior soon will be **DEPRECATED**.
See the new version [**Yii2 Linker Behavior**](https://github.com/voskobovich/yii2-linker-behavior).

About
-----

[](#about)

This behavior makes it easy to maintain many-to-many and one-to-many relations in your ActiveRecord models.

[![License](https://camo.githubusercontent.com/caa7238f26878fe8b8372cc155ca8f89e95d25ed80b1d3b8c085b59349bd7f5e/68747470733a2f2f706f7365722e707567782e6f72672f766f736b6f626f766963682f796969322d6d616e792d6d616e792d6265686176696f722f6c6963656e73652e737667)](https://packagist.org/packages/voskobovich/yii2-many-many-behavior)[![Latest Stable Version](https://camo.githubusercontent.com/6a7f4e8782c7624de47be09394147db38c63b62c65a712d54e33f2c389da9cda/68747470733a2f2f706f7365722e707567782e6f72672f766f736b6f626f766963682f796969322d6d616e792d6d616e792d6265686176696f722f762f737461626c652e737667)](https://packagist.org/packages/voskobovich/yii2-many-many-behavior)[![Latest Unstable Version](https://camo.githubusercontent.com/9e8382f95bb461bc85956bc8817f634b99c5305aa95daecc4a9deb159ca27438/68747470733a2f2f706f7365722e707567782e6f72672f766f736b6f626f766963682f796969322d6d616e792d6d616e792d6265686176696f722f762f756e737461626c652e737667)](https://packagist.org/packages/voskobovich/yii2-many-many-behavior)[![Total Downloads](https://camo.githubusercontent.com/af8a223265069dc3b33f56b2b8fda6f2bd18270ecf2ca5329c305bc95354bda3/68747470733a2f2f706f7365722e707567782e6f72672f766f736b6f626f766963682f796969322d6d616e792d6d616e792d6265686176696f722f646f776e6c6f6164732e737667)](https://packagist.org/packages/voskobovich/yii2-many-many-behavior)[![Build Status](https://camo.githubusercontent.com/72afc5ba893b39dac1760a6e5ef683499ec8e931c2853b176d3d8d458ddd113b/68747470733a2f2f7472617669732d63692e6f72672f766f736b6f626f766963682f796969322d6d616e792d746f2d6d616e792d6265686176696f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/voskobovich/yii2-many-to-many-behavior)

Support
-------

[](#support)

[GutHub issues](https://github.com/voskobovich/yii2-many-to-many-behavior/issues).

Usage
-----

[](#usage)

1. In your model, add the behavior and configure it
2. In your model, add validation rules for the attributes created by the behavior
3. In your view, create form fields for the attributes

Adding and configuring the behavior
-----------------------------------

[](#adding-and-configuring-the-behavior)

As an example, let's assume you are dealing with entities like `Book`, `Author` and `Review`. The `Book` model has the following relationships:

```
public function getAuthors()
{
    return $this->hasMany(Author::className(), ['id' => 'author_id'])
                ->viaTable('book_has_author', ['book_id' => 'id']);
}

public function getReviews()
{
    return $this->hasMany(Review::className(), ['id' => 'review_id']);
}
```

In the same model, the behaviour can be configured like so:

```
public function behaviors()
{
    return [
        [
            'class' => \voskobovich\behaviors\ManyToManyBehavior::className(),
            'relations' => [
                'author_ids' => 'authors',
				'review_ids' => 'reviews',
            ],
        ],
    ];
}
```

Relation names don't need to end in `_ids`, and you can use any name for a relation. It is recommended to use meaningful names, though.

Adding validation rules
-----------------------

[](#adding-validation-rules)

The attributes are created automatically. However, you must supply a validation rule for them (usually a `safe` validator):

```
public function rules()
{
    return [
        [['author_ids', 'review_ids'], 'each', 'rule' => ['integer']]
    ];
}
```

Creating form fields
--------------------

[](#creating-form-fields)

By default, the behavior will accept data from a multiselect field:

```
