yii2tech / ar-linkmany
为 Yii2 ActiveRecord 的多对多关系保存提供支持
Requires
- yiisoft/yii2: *
This package is auto-updated.
Last update: 2022-01-10 10:38:08 UTC
README
ActiveRecord 多对多保存扩展 for Yii2
此扩展提供了对 ActiveRecord 多对多关系保存的支持。例如:单个 "item" 可以属于多个 "groups",每个 group 可以与多个 items 相关联。与常规 [[\yii\db\BaseActiveRecord::link()]] 使用不同,此扩展会自动检查引用的存在性,删除排除的引用,并提供对网页表单组成的支持。
有关许可证信息,请查看 LICENSE 文件。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令
php composer.phar require --prefer-dist yii2tech/ar-linkmany
或向您的 composer.json 文件的 require 部分添加
"yii2tech/ar-linkmany": "*"
。
使用方法
此扩展提供了对 ActiveRecord 多对多关系保存的支持。这种支持是通过 [[\yii2tech\ar\linkmany\LinkManyBehavior]] ActiveRecord 行为提供的。您需要将其附加到您的 ActiveRecord 类中,并指定它的目标 "has-many" 关系
class Item extends ActiveRecord { public function behaviors() { return [ 'linkGroupBehavior' => [ 'class' => LinkManyBehavior::className(), 'relation' => 'groups', // relation, which will be handled 'relationReferenceAttribute' => 'groupIds', // virtual attribute, which is used for related records specification ], ]; } public static function tableName() { return 'Item'; } public function getGroups() { return $this->hasMany(Group::className(), ['id' => 'groupId'])->viaTable('ItemGroup', ['itemId' => 'id']); } }
附加 [[\yii2tech\ar\linkmany\LinkManyBehavior]] 会在拥有 ActiveRecord 中添加一个虚拟属性,其名称由 [[\yii2tech\ar\linkmany\LinkManyBehavior::$relationReferenceAttribute]] 确定。您可以通过此属性指定相关模型的主键
// Pick up related model primary keys: $groupIds = Group::find() ->select('id') ->where(['isActive' => true]) ->column(); $item = new Item(); $item->groupIds = $groupIds; // setup related models references $item->save(); // after main model is saved referred related models are linked
上面的例子等同于以下代码
$groups = Group::find() ->where(['isActive' => true]) ->all(); $item = new Item(); $item->save(); foreach ($groups as $group) { $item->link('groups', $group); }
注意:不要在拥有 ActiveRecord 类中声明
relationReferenceAttribute
属性。确保它不会与任何现有拥有字段或虚拟属性冲突。
通过 relationReferenceAttribute
声明的虚拟属性不仅用于保存,还包含关系的现有引用
$item = Item::findOne(15); var_dump($item->groupIds); // outputs something like: array(2, 5, 11)
您还可以在保存链接记录的同时编辑现有记录的引用列表
$item = Item::findOne(15); $item->groupIds = array_merge($item->groupIds, [17, 21]); $item->save(); // groups "17" and "21" will be added $item->groupIds = [5]; $item->save(); // all groups except "5" will be removed
注意:如果通过
relationReferenceAttribute
声明的属性从未被调用进行读取或写入,则它不会在拥有保存时进行处理。因此,它不会影响纯拥有保存。
创建关系设置网页界面
[[\yii2tech\ar\linkmany\LinkManyBehavior::$relationReferenceAttribute]] 的主要目的是支持创建多对多设置网页界面。您需要做的一切是在您的 ActiveRecord 中声明此虚拟属性的验证规则,以便可以从请求中收集其值
class Item extends ActiveRecord { public function rules() { return [ ['groupIds', 'safe'] // ensure 'groupIds' value can be collected on `populate()` // ... ]; } // ... }
在视图文件中,您应使用 relationReferenceAttribute
属性作为表单输入的属性名称
<?php use yii\helpers\ArrayHelper; use yii\helpers\Html; use yii\widgets\ActiveForm; /* @var $model Item */ ?> <?php $form = ActiveForm::begin(); ?> <?= $form->field($model, 'name'); ?> <?= $form->field($model, 'price'); ?> <?= $form->field($model, 'groupIds')->checkboxList(ArrayHelper::map(Group::find()->all(), 'id', 'name')); ?> <div class="form-group"> <?= Html::submitButton('Save', ['class' => 'btn btn-primary']) ?> </div> <?php ActiveForm::end(); ?>
在控制器中,您不需要任何特殊代码
use yii\web\Controller; class ItemController extends Controller { public function actionCreate() { $model = new Item(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view']); } return $this->render('create', [ 'model' => $model, ]); } // ... }