alexinator1/yii2-jta
Composer 安装命令:
composer require alexinator1/yii2-jta
包简介
Yii2 extension allows to access attributes of junction table and attach them as child model as properties in many-to-many relation
README 文档
README
A simple extension allows to access column values of junction table in ORM way without declaring additional model for that table in many-to-many relation. Extension overwrites \yii\db\ActiveQuery::viaTable() and allows to pass there array of column names of junction table which will be attached to child models as properties.
Requirements
- Yii 2.0
- PHP 5.4
Installation
The preferred way to install this extension is through Composer.
Either run
$ composer require alexinator1/yii2-jta
or add
"alexinator1/yii2-jta": "*"
to the require section of your composer.json file.
Usage
Just inherit your both model classes related in many-to-many relation from alexinator1\jta\ActiveRecord class.
Consider following scheme
class User extends \alexinator1\jta\ActiveRecord { .... }
class Group extends \alexinator1\jta\ActiveRecord { .... }
and pass array of attribute names you want to attach to child model to viaTable method
class Group extends \alexinator1\jta\ActiveRecord { ... public function getUsers() { return $this->hasMany(User::className(), ['id' => 'user_id']) ->viaTable('user_group', ['group_id' => 'id'], null, ['role', 'joined_at']); } ... }
That's it. Now child model has a property which named as pivot attribute and contains array of corresponding values indexed by parent id. So it can't be accessed following way:
Lazy loading:
$group = Group::findOne($groupId); foreach($group->users as $user) { $role = $user->role[$group->id]; $joinDate = $user->joined_at[$group->id]; ... }
Eager loading:
$group = Group::find()->where($groupId)->with('users')->all(); foreach($group->users as $user) { $role = $user->role[$group->id]; $joinDate = $user->joined_at[$group->id]; ... }
Eager loading using 'joinWith' methods:
$groups = Group::find()->joinWith('users')->all(); foreach($groups as $group){ foreach($group->users as $user) { $role = $user->role[$group->id]; ... } }
works with 'array' models as well:
$group = Group::find() ->with('users') ->where($groupId) ->asArray() ->one(); foreach($group['users'] as $user) { $role = $user['role']; $joinDate = $user['joined_at']; ... }
Show case page!
You may find more code samples in DEMO page. And some unit tests on DEMO page repository
Note!
Attached pivot attributes are read-only and acceptable only for models
were populated via relation. They overwrite all other none-declared model properties
(declared via getter or corresponded to table columns)
and are overwritten by declared properties.
Failed use cases
If you find any usecases where extension doesn't work properly. Please feel free to issue it or send me to email. We will try to handle it ASAP.
License
**yii2 junction table attributes ** is released under the MIT License. See the bundled LICENSE.md for details.
Resources
alexinator1/yii2-jta 适用场景与选型建议
alexinator1/yii2-jta 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.76k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2015 年 09 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「activerecord」 「yii2」 「many-to-many」 「Junction table」 「Pivot table」 「Junction Attributes」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 alexinator1/yii2-jta 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alexinator1/yii2-jta 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 alexinator1/yii2-jta 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
ActiveRecord for API
Many-to-many ActiveRecord relation for Yii 2 framework
CalendarView widget for Yii 2 Framework.
A custom URL rule class for Yii 2 which allows to create translated URL rules
Easy linking and sync relationships many-to-many and one-to-many
Saving related data in Yii2
统计信息
- 总下载量: 7.76k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-09-13
