folkpro/yii2-many-has-many-behaviors

Yii2 行为

dev-master 2015-11-10 10:32 UTC

This package is not auto-updated.

Last update: 2024-09-28 18:58:37 UTC


README

安装

安装此扩展的首选方式是通过 Composer

可以运行

composer require folkpro/yii2-many-has-many-behaviors

或者将以下内容添加到你的应用程序的 composer.json 文件中的 require 部分:

"folkpro/yii2-many-has-many-behaviors" : "*"

to the require section of your application's composer.json file.

用法

  • 在你的模型中添加行为并配置它
public function behaviors()
{
    return [
        [
            'class' => \folkpro\manyhasmanybehaviors\ManyHasManyBehavior::className(),
            'relations' => [
                 'tags' => 'tag_items',                  
             ],
        ],
    ];
}
  • 在你的模型中添加关系,例如
public function getTags()
{
    return $this->hasMany(Tag::className(), ['id' => 'tag_id'])
        ->viaTable('post_has_tag', ['post_id' => 'id']);
}
  • 在你的模型中添加由行为创建的属性的有效性规则,例如
public function rules()
{
    return [
        [['tag_list'], 'safe']
    ];
}
  • 在你的视图中为属性创建表单字段

更多信息请在这里
ManyHasManyBehavior