suver/yii2-behavior-subset

行为子集

安装次数: 54

依赖项: 0

建议者: 0

安全性: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

语言:HTML

类型:yii2-extension

1.1.2 2019-04-29 13:27 UTC

This package is not auto-updated.

Last update: 2024-09-15 02:07:53 UTC


README

行为子集

安装

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

运行以下命令之一:

php composer.phar require suver/yii2-behavior-subset

或者

"suver/yii2-behavior-subset": "*"

将以下内容添加到您的 composer.json 文件的 require 部分。

配置

对于多对多关系,您的行为部分可以这样写:

public function behaviors()
{
    return [
        [
            'class' => '\suver\behavior\Subset',
            'relation' => 'authors', // you relation
            'attribute' => 'authors_ids',
        ]
    ];
}

/**
 * Relation with Other Model
 *
 * @return \yii\db\ActiveQuery
 */
public function getAuthors()
{
    return $this->hasMany(OtherModel::className(), ['id' => 'other_model_id'])->viaTable('this_model_to_other_model', ['this_model_id' => 'id']);
}

使用方法

扩展安装后,只需在代码中通过

// save relation
$model->authors_ids = [1,2,3,4];
$model->save();

// get realtion
var_dump($model->authors)

yii2-behavior-subset

行为子集。轻松处理多对多关系。