悉达多/yii2-relational-set

此软件包最新版本(dev-master)没有提供许可信息。

将 Yii2 m2m 连接关系表示为 PK 数组字段

dev-master 2017-04-05 09:22 UTC

This package is not auto-updated.

Last update: 2024-09-15 02:28:11 UTC


README

将 Yii2 m2m 连接关系表示为数组字段。存储其更改作为差异而不清除关系。

安装

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

运行以下命令之一:

$ composer require siddthartha/yii2-relational-set

"siddthartha/yii2-relational-set": "*"

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

使用方法

主机模型

class Host extends \yii\db\ActiveRecord
{
    public $_slaves;

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'sets' => [
                'class' => \siddthartha\behaviors\RelationalSetBehavior::class,
                'attributes' => [
                    '_slaves' => 'slaves',
                ],
            ],
        ];
    }

    /**
     * @return \yii\db\ActiveQueryInterface
     */
    public function getSlaves()
    {
        return $this->hasMany(Slave::class, ['id' => 'id_slave'])
            ->viaTable(HostSlave::tableName(), ['id_host' => 'id'])
            ->indexBy('id');
    }
}

查看代码示例

    <?=$form->field( $model, '_slaves')->checkboxList(/*...*/)?>

对关系的任何更改(连接表)都只会作为 insert 和/或 update 需要的差异来执行!