PHPackages                             thtmorais/yii2-many-2-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. thtmorais/yii2-many-2-many

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

thtmorais/yii2-many-2-many
==========================

0.0.2(12mo ago)02.4k↓45%MITPHP

Since Feb 12Pushed 12mo ago1 watchersCompare

[ Source](https://github.com/thtmorais/yii2-many-2-many)[ Packagist](https://packagist.org/packages/thtmorais/yii2-many-2-many)[ RSS](/packages/thtmorais-yii2-many-2-many/feed)WikiDiscussions main Synced 1mo ago

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

Many To Many (many-2-many) implementation for Yii PHP Framework 2
-----------------------------------------------------------------

[](#many-to-many-many-2-many-implementation-for-yii-php-framework-2)

Overview
--------

[](#overview)

`Many2ManyBehavior` is a Yii PHP Framework 2 behavior that facilitates managing many-2-many relationships between models. It automatically handles the insertion, update, and deletion of related models.

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

[](#installation)

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

Either run

```
composer require thtmorais/yii2-many-2-many "*"
```

or add

```
"thtmorais/yii2-many-2-many": "*"

```

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

Usage
-----

[](#usage)

Attach `Many2ManyBehavior` to your model:

```
use thtmorais\many2many\Many2ManyBehavior;

class Model extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            [
                'class' => Many2ManyBehavior::class,
                'id' => 'id',
                'attribute' => 'relations',
                'relatedModel' => RelatedModel::class,
                'relatedModelId' => 'id',
                'relatedModelAttribute' => 'model_id',
                'relatedModelValidate' => true
            ],
        ];
    }
}
```

Properties
----------

[](#properties)

PropertyTypeDescription`id``string`Primary key of the source model. Defaults to `'id'`.`attribute``string`Attribute containing the relationships to be saved.`relatedModel``string`Class name of the related model.`relatedModelId``string`Primary key of the related model. Defaults to `'id'`.`relatedModelAttribute``string`Foreign key in the related model referencing the source model.`relatedModelValidate``bool`Whether to validate the related model before saving. Defaults to `true`.Events Handled
--------------

[](#events-handled)

- **`EVENT_AFTER_INSERT`**: Saves related models after the source model is inserted.
- **`EVENT_AFTER_UPDATE`**: Updates related models when the source model is updated.
- **`EVENT_AFTER_DELETE`**: Deletes related models when the source model is deleted.

Methods
-------

[](#methods)

### `afterInsert()`

[](#afterinsert)

Handles the insertion of related models when the source model is inserted.

### `afterUpdate()`

[](#afterupdate)

Handles the update of related models when the source model is updated.

### `afterDelete()`

[](#afterdelete)

Deletes related models when the source model is deleted.

Example Usage
-------------

[](#example-usage)

```
$model = new Model();

$model->relations = [
    ['id' => 1, 'name' => 'Item 1'],
    ['id' => 2, 'name' => 'Item 2']
];

$model->save();
```

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance50

Moderate activity, may be stable

Popularity21

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity29

Early-stage or recently created project

 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 ~100 days

Total

2

Last Release

360d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b7ce64ebc7a8f5e6a566ea03832888b01e938a0090e23d0cde0ac266538d1dbb?d=identicon)[thtmorais](/maintainers/thtmorais)

---

Top Contributors

[![thtmorais](https://avatars.githubusercontent.com/u/18515639?v=4)](https://github.com/thtmorais "thtmorais (7 commits)")

---

Tags

m2mmany-2-manymany-to-manymtmyii2yii2-extensionyii2-many-2-manyyii2-many-to-manyyii2yii2-extensionmany to manyMTMm2mmany-2-manyyii2-many-2-manyyii2-many-to-many

### Embed Badge

![Health badge](/badges/thtmorais-yii2-many-2-many/health.svg)

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

###  Alternatives

[skeeks/cms

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

13825.6k47](/packages/skeeks-cms)[devanych/yii2-cart

Shopping cart for Yii2

2011.2k](/packages/devanych-yii2-cart)

PHPackages © 2026

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