https://github.com/jsas4coding/yii2-m2m-behavior
Yii2 behavior for managing many-to-many (M:N) relationships using ActiveRecord with virtual attributes, auto-syncing, and full test coverage.
https://github.com/jsas4coding/yii2-m2m-behavior
active-record behavior junction-table many-to-many php relations yii2 yii2-extension
Last synced: 3 months ago
JSON representation
Yii2 behavior for managing many-to-many (M:N) relationships using ActiveRecord with virtual attributes, auto-syncing, and full test coverage.
- Host: GitHub
- URL: https://github.com/jsas4coding/yii2-m2m-behavior
- Owner: jsas4coding
- License: mit
- Created: 2025-04-17T12:44:59.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2025-04-21T23:38:53.000Z (6 months ago)
- Last Synced: 2025-06-02T00:56:30.044Z (4 months ago)
- Topics: active-record, behavior, junction-table, many-to-many, php, relations, yii2, yii2-extension
- Language: PHP
- Homepage:
- Size: 112 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
- Security: SECURITY.md
- Roadmap: ROADMAP.md
Awesome Lists containing this project
README
# Yii2 Many to Many Behavior
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
```bash
composer require jonatas-sas/yii2-m2m-behavior
```---
## 📚 Documentation
- 📘 [English Docs](docs/index.md)
- 🇧🇷 [Documentação em Português](docs/index.pt_BR.md)---
## 🚀 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+)
```php
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
```php
echo $form->field($model, 'tagIds')->checkboxList(
Tag::find()
->select(['name', 'id'])
->indexBy('id')
->column()
);
```---
## 🤝 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
Yii2 Many to Many Behavior is released under the MIT License.
---
## 💙 Credits
Maintained by the Yii2 community.\
Inspired by the Yii2Tech package and rebuilt with care for modern development.---