yii2tech/ar-linkmany
最新稳定版本:1.0.3
Composer 安装命令:
composer require yii2tech/ar-linkmany
包简介
Provides support for many-to-many relations saving in Yii2 ActiveRecord
README 文档
README
ActiveRecord Many-to-Many Saving Extension for Yii2
This extension provides support for ActiveRecord many-to-many relation saving. For example: single "item" may belong to several "groups", each group may be linked with several items. Unlike regular [[\yii\db\BaseActiveRecord::link()]] usage, this extension automatically checks references existence, removes excluded references and provide support for web form composition.
For license information check the LICENSE-file.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist yii2tech/ar-linkmany
or add
"yii2tech/ar-linkmany": "*"
to the require section of your composer.json.
Usage
This extension provides support for ActiveRecord many-to-many relation saving. This support is granted via [[\yii2tech\ar\linkmany\LinkManyBehavior]] ActiveRecord behavior. You'll need to attach it to your ActiveRecord class and point the target "has-many" relation for it:
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']); } }
Being attached [[\yii2tech\ar\linkmany\LinkManyBehavior]] adds a virtual proprty to the owner ActiveRecord, which name is determined by [[\yii2tech\ar\linkmany\LinkManyBehavior::$relationReferenceAttribute]]. You will be able to specify related models primary keys via this attribute:
// 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
The above example is equal to the following code:
$groups = Group::find() ->where(['isActive' => true]) ->all(); $item = new Item(); $item->save(); foreach ($groups as $group) { $item->link('groups', $group); }
Attention: do NOT declare
relationReferenceAttributeattribute in the owner ActiveRecord class. Make sure it does not conflict with any existing owner field or virtual property.
Virtual property declared via relationReferenceAttribute serves not only for saving. It also contains existing references
for the relation:
$item = Item::findOne(15); var_dump($item->groupIds); // outputs something like: array(2, 5, 11)
You may as well edit the references list for existing record, while saving linked records will be synchronized:
$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
Note: if attribute declared by
relationReferenceAttributeis never invoked for reading or writing, it will not be processed on owner saving. Thus it will not affect pure owner saving.
Creating relation setup web interface
The main purpose of [[\yii2tech\ar\linkmany\LinkManyBehavior::$relationReferenceAttribute]] is support for creating many-to-many setup web interface. All you need to do is declare a validation rule for this virtual property in your ActiveRecord, so its value can be collected from the request:
class Item extends ActiveRecord { public function rules() { return [ ['groupIds', 'safe'] // ensure 'groupIds' value can be collected on `populate()` // ... ]; } // ... }
Inside the view file you should use relationReferenceAttribute property as an attribute name for the form input:
<?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(); ?>
Inside the controller you don't need any special code:
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, ]); } // ... }
yii2tech/ar-linkmany 适用场景与选型建议
yii2tech/ar-linkmany 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 301.68k 次下载、GitHub Stars 达 83, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「link」 「active」 「record」 「yii2」 「many-to-many」 「many」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yii2tech/ar-linkmany 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yii2tech/ar-linkmany 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yii2tech/ar-linkmany 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A couple of handy form fields and objects for managing external and internal links on DataObjects
Propel2 is an open-source Object-Relational Mapping (ORM) for PHP 5.5 and up.
PHP Interface for Babel Street Text Analytics
PHP library that allows linking queries from diferent physical databases using mysql pdo database connections
A Symfony extension to get active class base on current bundle/controller/action
Custom links on your sidebar for Laravel Nova.
统计信息
- 总下载量: 301.68k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 85
- 点击次数: 21
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 未知