PHPackages                             evgeny-gavrilov/yii2-many-to-many - 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. [Framework](/categories/framework)
4. /
5. evgeny-gavrilov/yii2-many-to-many

ActiveYii2-extension[Framework](/categories/framework)

evgeny-gavrilov/yii2-many-to-many
=================================

Extension many to many relation for Yii2 Framework.

v1.1.0(10y ago)31.6k1MTIPHP

Since Apr 18Pushed 10y ago3 watchersCompare

[ Source](https://github.com/EvgenyGavrilov/yii2-many-to-many)[ Packagist](https://packagist.org/packages/evgeny-gavrilov/yii2-many-to-many)[ RSS](/packages/evgeny-gavrilov-yii2-many-to-many/feed)WikiDiscussions master Synced 1mo ago

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

Yii2 many to many behavior
--------------------------

[](#yii2-many-to-many-behavior)

[![Build Status](https://camo.githubusercontent.com/b1575e159a4d39cd91d7bc594d7f53614f936409d6ab3048cc8fcc0f98a58d7a/68747470733a2f2f7472617669732d63692e6f72672f457667656e7947617672696c6f762f796969322d6d616e792d746f2d6d616e792e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/EvgenyGavrilov/yii2-many-to-many)[![Packagist](https://camo.githubusercontent.com/a4b7a1d8377c4252b62962b12857454a352b488c7f58770dcbcadcffaf57247b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f646f637472696e652f6f726d2e737667)](https://packagist.org/packages/evgeny-gavrilov/yii2-many-to-many)[![Code Climate](https://camo.githubusercontent.com/aab1a563dd5707ef315632b0ca0870b3691ea39b83e610c748e50677fc8a9f6f/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f457667656e7947617672696c6f762f796969322d6d616e792d746f2d6d616e792f6261646765732f6770612e737667)](https://codeclimate.com/github/EvgenyGavrilov/yii2-many-to-many)[![Test Coverage](https://camo.githubusercontent.com/a2b74a623ac95b683dde3b4ab6131a17e7281725fc10b09522d5558af17d7efb/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f457667656e7947617672696c6f762f796969322d6d616e792d746f2d6d616e792f6261646765732f636f7665726167652e737667)](https://codeclimate.com/github/EvgenyGavrilov/yii2-many-to-many)

This behavior allows to update or delete relations in the junction table, which are described in the model with [via()](http://www.yiiframework.com/doc-2.0/yii-db-activerelationtrait.html#via%28%29-detail)or [viaTable()](http://www.yiiframework.com/doc-2.0/yii-db-activequery.html#viaTable%28%29-detail).

Behavior doesn't use transaction. Use this method to do [transactions()](http://www.yiiframework.com/doc-2.0/yii-db-activerecord.html#transactions%28%29-detail)in the model or other way.

Install
=======

[](#install)

```
php composer.phar require evgeny-gavrilov/yii2-many-to-many

```

Usage
=====

[](#usage)

Connection of behavior and description of the relation in the model
-------------------------------------------------------------------

[](#connection-of-behavior-and-description-of-the-relation-in-the-model)

```
class User extends ActiveRecord
{
    public function behaviors()
    {
        return [
            EvgenyGavrilov\behavior\ManyToManyBehavior::className()
        ];
    }

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

Usage in the code
-----------------

[](#usage-in-the-code)

```
$user = User::findOne(1);
// Add new or update relation
$model->setRelated('groups', [1]);
// or
$model->setRelated('groups', [1, 2]);
$model->save();

// Add or update the data with the remove old relations
$model->setRelated('groups', [1, 2], true);
$model->save();

// Delete all relations
$model->setRelated('groups', [], true);
$model->save();
```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~221 days

Total

2

Last Release

3827d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/3016130?v=4)[Evgeny Gavrilov](/maintainers/EvgenyGavrilov)[@EvgenyGavrilov](https://github.com/EvgenyGavrilov)

---

Top Contributors

[![EvgenyGavrilov](https://avatars.githubusercontent.com/u/3016130?v=4)](https://github.com/EvgenyGavrilov "EvgenyGavrilov (1 commits)")

---

Tags

yii2Behaviormany to manyrelation

### Embed Badge

![Health badge](/badges/evgeny-gavrilov-yii2-many-to-many/health.svg)

```
[![Health](https://phpackages.com/badges/evgeny-gavrilov-yii2-many-to-many/health.svg)](https://phpackages.com/packages/evgeny-gavrilov-yii2-many-to-many)
```

###  Alternatives

[voskobovich/yii2-many-many-behavior

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

112181.9k2](/packages/voskobovich-yii2-many-many-behavior)[arogachev/yii2-many-to-many

Many-to-many ActiveRecord relation for Yii 2 framework

3541.2k4](/packages/arogachev-yii2-many-to-many)[voskobovich/yii2-linker-behavior

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

80319.0k9](/packages/voskobovich-yii2-linker-behavior)[skeeks/cms

SkeekS CMS — control panel and tools based on php framework Yii2

13825.6k47](/packages/skeeks-cms)[arogachev/yii2-sortable

Sortable ActiveRecord for Yii 2 framework

1637.0k4](/packages/arogachev-yii2-sortable)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
