PHPackages                             jonatas-sas/yii2-m2m-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. jonatas-sas/yii2-m2m-behavior

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

jonatas-sas/yii2-m2m-behavior
=============================

A Yii2 behavior to easily manage many-to-many relations using ActiveRecord.

v2.1.0(1y ago)76[1 PRs](https://github.com/jsas4coding/yii2-m2m-behavior/pulls)MITPHPPHP &gt;=8.1 &lt;8.5.0CI passing

Since Apr 17Pushed 8mo ago1 watchersCompare

[ Source](https://github.com/jsas4coding/yii2-m2m-behavior)[ Packagist](https://packagist.org/packages/jonatas-sas/yii2-m2m-behavior)[ RSS](/packages/jonatas-sas-yii2-m2m-behavior/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (6)Versions (11)Used By (0)

Yii2 Many to Many Behavior
==========================

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

 [![PHP 8.1+](https://camo.githubusercontent.com/194bda91bda289595fe647b04e129ab10739c3ceae5d0650842ab246687e4670/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e312b2d3838393242462e7376673f7374796c653d666c61742d737175617265266c6f676f3d706870)](https://www.php.net/releases/8.1/en.php "PHP Version 8.1+") [![Powered by Yii Framework](https://camo.githubusercontent.com/caa07fe5c2d65aec86e62e1957ad3df612d53c4f055c8209179f12407e806242/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f506f77657265645f62792d5969695f4672616d65776f726b2d677265656e2e7376673f7374796c653d666c61742d737175617265)](https://www.yiiframework.com/ "Yii Framework Website") [![Packagist Version](https://camo.githubusercontent.com/882e1dcaa0792bd2ec4517f6d1b55bd3af8afb62c7e76da7c3c492d4dd21da7c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a6f6e617461732d7361732f796969322d6d326d2d6265686176696f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jonatas-sas/yii2-m2m-behavior "View on Packagist") [![License](https://camo.githubusercontent.com/00d1be5a91b0065cc55e64cf61b37ea08f65e9c52834300d6771c1cdaa21238f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6a6f6e617461732d7361732f796969322d6d326d2d6265686176696f722e7376673f7374796c653d666c61742d737175617265)](LICENSE "View License")

 [![Lint Status](https://github.com/jonatas-sas/yii2-m2m-behavior/actions/workflows/lint.yml/badge.svg)](https://github.com/jonatas-sas/yii2-m2m-behavior/actions/workflows/lint.yml "Lint Workflow") [![Static Analysis](https://github.com/jonatas-sas/yii2-m2m-behavior/actions/workflows/static.yml/badge.svg)](https://github.com/jonatas-sas/yii2-m2m-behavior/actions/workflows/static.yml "Static Analysis Status") [![Tests Status](https://github.com/jonatas-sas/yii2-m2m-behavior/actions/workflows/test.yml/badge.svg)](https://github.com/jonatas-sas/yii2-m2m-behavior/actions/workflows/test.yml "Test Workflow")

 [![Security Status](https://github.com/jonatas-sas/yii2-m2m-behavior/actions/workflows/security.yml/badge.svg)](https://github.com/jonatas-sas/yii2-m2m-behavior/actions/workflows/security.yml "Security Scan") [![Dependabot](https://github.com/jonatas-sas/yii2-m2m-behavior/actions/workflows/dependabot/dependabot-updates/badge.svg)](https://github.com/jonatas-sas/yii2-m2m-behavior/actions/workflows/dependabot/dependabot-updates "Dependabot Updates") [![Coverage](https://camo.githubusercontent.com/de7e1acd98d1024b58983ba21c55a9a98a3210c0be6538877bbcad2493d2d13f/68747470733a2f2f636f6465636f762e696f2f67682f6a6f6e617461732d7361732f796969322d6d326d2d6265686176696f722f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/jonatas-sas/yii2-m2m-behavior "Code Coverage")

 [![Total Downloads](https://camo.githubusercontent.com/16c3ad00afd3645204f154d899786c4df6940ef559da1a4ee4fd15d09057c32c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a6f6e617461732d7361732f796969322d6d326d2d6265686176696f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jonatas-sas/yii2-m2m-behavior/stats "Total Downloads") [![Open Issues](https://camo.githubusercontent.com/7cba97f78fc2fb4a2beeb1a7ff6ee37d7b7b2704c5a236337f667aa9a08c3a95/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f6a6f6e617461732d7361732f796969322d6d326d2d6265686176696f722e7376673f7374796c653d666c61742d737175617265)](https://github.com/jonatas-sas/yii2-m2m-behavior/issues "Open Issues") [![Open Pull Requests](https://camo.githubusercontent.com/8811a3bdd2911a1daf0f7f0f8a959fdc6500866f0cc1158ec8d0b55818a11b85/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732d70722f6a6f6e617461732d7361732f796969322d6d326d2d6265686176696f722e7376673f7374796c653d666c61742d737175617265)](https://github.com/jonatas-sas/yii2-m2m-behavior/pulls "Open Pull Requests")

A reusable and robust behavior for managing many-to-many (M2M) relationships in **Yii2 ActiveRecord** using virtual attributes.

> 🧩 Inspired by the archived [`yii2tech/ar-linkmany`](https://github.com/yii2tech/ar-linkmany) package by [Paul Klimov](https://github.com/PaulKlimov), now extended with modern improvements, full test coverage, and long-term support.

---

📦 Installation
--------------

[](#-installation)

```
composer require jonatas-sas/yii2-m2m-behavior
```

---

📚 Documentation
---------------

[](#-documentation)

- 📘 [English Docs](docs/index.md)
- 🇧🇷 [Documentação em Português](docs/index.pt_BR.md)

---

🚀 Overview
----------

[](#-overview)

Yii2 Many to Many Behavior helps you:

- Manage M2M relations using **virtual attributes** (e.g. `tagIds`).
- Automatically sync relations on `insert`, `update`, and `delete`.
- Control deletion of junction table rows (`deleteOnUnlink`).
- Add **extra columns** to junction records (e.g. timestamps or metadata).
- Integrate smoothly into **ActiveForm**, **GridView**, and **DetailView**.

---

🛠 Example Usage (PHP 8.1+)
--------------------------

[](#-example-usage-php-81)

```
use yii\db\ActiveRecord;
use yii\db\ActiveQuery;
use odara\yii\behaviors\LinkManyToManyBehavior;

/**
 * @property int        $id
 * @property string     $name
 *
 * @property-read Tag[] $tags
 * @property int[]      $tagIds
 */
class Item extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'tags' => [
                'class' => LinkManyToManyBehavior::class,
                'relation' => 'tags',
                'referenceAttribute' => 'tagIds',
                'deleteOnUnlink' => true,
                'extraColumns' => [
                    'source' => 'admin',
                    'created_at' => static fn (): int => time(),
                ],
            ],
        ];
    }

    /**
     * Returns the relation between Item and Tag models.
     *
     * @return ActiveQuery
     */
    public function getTags(): ActiveQuery
    {
        return $this->hasMany(Tag::class, ['id' => 'tag_id'])
            ->viaTable('item_tag', ['item_id' => 'id']);
    }
}
```

### Example Form Field

[](#example-form-field)

```
echo $form->field($model, 'tagIds')->checkboxList(
    Tag::find()
        ->select(['name', 'id'])
        ->indexBy('id')
        ->column()
);
```

---

🤝 Contributing
--------------

[](#-contributing)

Found a bug or want to suggest an improvement?

- Read the [Contributing Guide](CONTRIBUTING.md)
- Follow [PSR-12](https://www.php-fig.org/psr/psr-12/) and [Yii2 coding practices](https://www.yiiframework.com/doc/guide/2.0/en)

---

🛡 License
---------

[](#-license)

Yii2 Many to Many Behavior is released under the MIT License.

---

💙 Credits
---------

[](#-credits)

Maintained by the Yii2 community.
Inspired by the Yii2Tech package and rebuilt with care for modern development.

---

 [![Yii Framework](https://camo.githubusercontent.com/6f0a7c6c5e9ed8d389db5c82af26a2f70418756b736357378178997e52296488/68747470733a2f2f7777772e7969696672616d65776f726b2e636f6d2f696d6167652f64657369676e2f6c6f676f2f796969335f66756c6c5f666f725f6461726b2e737667 "Yii Framework")](https://www.yiiframework.com "Yii Framework")

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance56

Moderate activity, may be stable

Popularity10

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 97.1% 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 ~0 days

Total

9

Last Release

386d ago

Major Versions

v1.3.1 → v2.0.02025-04-19

PHP version history (2 changes)1.0.0PHP &gt;=7.4 &lt;8.4.0

v2.0.0PHP &gt;=8.1 &lt;8.5.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/0d4f114852e07746cdd94467223b22cbfe7f58be518d197ca5c244a49ca5cc5c?d=identicon)[jonatas-sas](/maintainers/jonatas-sas)

---

Top Contributors

[![jsas4coding](https://avatars.githubusercontent.com/u/4156892?v=4)](https://github.com/jsas4coding "jsas4coding (68 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (2 commits)")

---

Tags

active-recordbehaviorjunction-tablemany-to-manyphprelationsyii2yii2-extensionyii2extensionBehaviorrelationsmany to many

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/jonatas-sas-yii2-m2m-behavior/health.svg)

```
[![Health](https://phpackages.com/badges/jonatas-sas-yii2-m2m-behavior/health.svg)](https://phpackages.com/packages/jonatas-sas-yii2-m2m-behavior)
```

###  Alternatives

[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)[sjaakp/yii2-sortable-behavior

Sort ActiveRecords and related records in Yii2.

36144.7k](/packages/sjaakp-yii2-sortable-behavior)[nanson/yii2-postgis

Yii2-extension to work with postgis data

1851.6k](/packages/nanson-yii2-postgis)[dmstr/yii2-db

Database extensions

19618.8k6](/packages/dmstr-yii2-db)

PHPackages © 2026

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