vadymsemeniuk/yii2-many-to-many-ar-level-save-behavior
Composer 安装命令:
composer require vadymsemeniuk/yii2-many-to-many-ar-level-save-behavior
包简介
Behavior for saving many to many relation
README 文档
README
Yii2 ManyToMany Active Record Level Behavior
Extension save many-to-many related data
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist vadymsemeniuk/yii2-many-to-many-ar-level-save-behavior "*"
or add
"vadymsemeniuk/yii2-many-to-many-ar-level-save-behavior": "*"
to the require section of your composer.json file.
Usage
An example of usage could be:
Suppose you have the following schema.
CREATE TABLE `article` ( `id` int(11) NOT NULL AUTO_INCREMENT, `label` varchar(1024) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`), ); CREATE TABLE `tag` ( `id` int(11) NOT NULL AUTO_INCREMENT, `label` varchar(1024) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`), ); CREATE TABLE `article_to_tag` ( `id` int(11) NOT NULL AUTO_INCREMENT, `article_id` int(11) NOT NULL, `tag_id` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `fk-article_to_tag-article_id-article-id` (`article_id`), KEY `fk-article_to_tag-tag_id-tag-id` (`tag_id`), CONSTRAINT `fk-article_to_tag-article_id-article-id` FOREIGN KEY (`article_id`) REFERENCES `article` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `fk-article_to_tag-tag_id-tag-id` FOREIGN KEY (`tag_id`) REFERENCES `tag` (`id`) ON DELETE CASCADE ON UPDATE CASCADE )
Your code must look like this
use vadymsemenykv\manyToManyBehavior\ManyToManyArLevelSaveBehavior /** * @ property Tag[] $tags */ class Article extends ActiveRecord { public $tagIds = []; public function tableName() { return 'article'; } public function getTags() { return $this->hasMany(Tag::className(), ['id' => 'tag_id']) ->viaTable('article_to_tag', ['article_id' => 'id']); } public function behaviors() { return [ 'manyToManyBehavior' => [ 'class' => ManyToManyArLevelSaveBehavior::className(), 'relations' => [ 'tags' => [ 'modelClass' => Tag::className(), 'attribute' => 'tagIds', 'pkColumnName' => 'id', 'deleteAllRelatedEntriesBeforeSave' => true, ], ], ], ]; } }
class Tag extends ActiveRecord { public function tableName() { return 'tag'; } }
$tag = new Tag(); $tag->id = 1; $tag->label = 'Tag #1'; $tag->save(); $tag = new Tag(); $tag->id = 2; $tag->label = 'Tag #2'; $tag->save(); $article = new Article(); $article->label = 'New article'; $article->tagIds = [1, 2]; $article->save(); $tags = $article->tags; /** * $tags = [ * 0 => object(Tag) * ... * private '_attributes' (yii\db\BaseActiveRecord) => * 'id' => int 1 * 'label' => string 'Tag #1' * ... * 1 => object(Tag) * ... * private '_attributes' (yii\db\BaseActiveRecord) => * 'id' => int 2 * 'label' => string 'Tag #2' * ... * ] */
Advanced usage
For example, if in table article_to_tag in example above present type_id column with some additional
identifier - config for behavior may be like this:
public function behaviors()
{
return [
'manyToManyBehavior' => [
'class' => ManyToManyArLevelSaveBehavior::className(),
'relations' => [
'tags' => [
'modelClass' => Tag::className(),
'attribute' => 'tagIds',
'pkColumnName' => 'id',
'deleteAllRelatedEntriesBeforeSave' => true,
'extraColumns' => [
'type_id' => ArticleToTag::TYPE_1
],
'additionalUnlinkCondition' => [
'type_id' => ArticleToTag::TYPE_1,
]
],
],
],
];
}
where extraColumns will be used in link() method, and additionalUnlinkCondition will be merged with unlink condition.
vadymsemeniuk/yii2-many-to-many-ar-level-save-behavior 适用场景与选型建议
vadymsemeniuk/yii2-many-to-many-ar-level-save-behavior 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 72.16k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「extension」 「Behavior」 「yii2」 「saving」 「manytomany」 「many to many」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vadymsemeniuk/yii2-many-to-many-ar-level-save-behavior 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vadymsemeniuk/yii2-many-to-many-ar-level-save-behavior 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vadymsemeniuk/yii2-many-to-many-ar-level-save-behavior 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A CakePHP plugin to provide easy access to zumba/swivel
A custom URL rule class for Yii 2 which allows to create translated URL rules
This is a set of Yii2 behaviors extensions
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
UI Kit 3 Extension for Yii2
统计信息
- 总下载量: 72.16k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04