承接 jonatas-sas/yii2-m2m-behavior 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jonatas-sas/yii2-m2m-behavior

最新稳定版本:v2.1.0

Composer 安装命令:

composer require jonatas-sas/yii2-m2m-behavior

包简介

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

README 文档

README

PHP 8.1+ Powered by Yii Framework Packagist Version License

Lint Status Static Analysis Tests Status

Security Status Dependabot Coverage

Total Downloads Open Issues 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 package by Paul Klimov, now extended with modern improvements, full test coverage, and long-term support.

📦 Installation

composer require jonatas-sas/yii2-m2m-behavior

📚 Documentation

🚀 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+)

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

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

🤝 Contributing

Found a bug or want to suggest an improvement?

🛡 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.

Yii Framework

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-17