cornernote / yii2-linkall
处理在Yii2中保存多个多对多相关记录的行为。
1.0.0
2015-06-20 08:36 UTC
Requires
- yiisoft/yii2: *
Requires (Dev)
- phpunit/dbunit: ~1.0
- phpunit/phpunit: 4.*
- scrutinizer/ocular: ~1.1
This package is auto-updated.
Last update: 2024-09-19 18:53:56 UTC
README
处理在Yii2中保存多个多对多相关记录的行为。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一:
$ composer require cornernote/yii2-linkall "*"
或者将以下内容添加到您的 composer.json
文件的 require
部分:
"cornernote/yii2-linkall": "*"
```
使用方法
帖子模型
class Post extends ActiveRecord { public function behaviors() { return [ \cornernote\linkall\LinkAllBehavior::className(), ]; } public function getTags() { return $this->hasMany(Tag::className(), ['id' => 'tag_id']) ->viaTable('post_to_tag', ['post_id' => 'id']); //->via('postToTag'); } }
标签模型
class Tag extends ActiveRecord { }
帖子控制器
class PostController extends Controller { public function actionExample() { $post = Post::findOne(1); $tags = [Tag::findOne(2), Tag::findOne(3)]; $extraColumns = []; // extra columns to be saved to the many to many table $unlink = true; // unlink tags not in the list $delete = true; // delete unlinked tags $post->linkAll('tags', $tags, $extraColumns, $unlink, $delete); } }