udokmeci/yii2-closest-relation-trait
Composer 安装命令:
composer require udokmeci/yii2-closest-relation-trait
包简介
This trait adds capability to fetch nearest related model class without need of redefininf relations on extensions.
README 文档
README
This trait adds capability to fetch nearest related model class without need of redefininf relations on extensions.
Why?
I like using gii however if you have frequently changed table like me then it is a pain to continue using gii or adding changes by hand.
And if you are using yii2 with more than one app you have to move relations as you extend models.
This trait (yes I have used trait instead of behaviours) add capability to call nearest Class sharing namespace of the model and its parents.
For example, let's say we are using yii2-advance-template so we may have 3 or more childs of same class.
call the class Page and relation to Author
you probably has it in common\models, frontend\models and backend\models so if you don't override the relations you probably get common\models\Author with getter. It is easy to copy and paste relations however adding a trait to your base model is easier.
How to use?
##Installation with Composer
Just add the line under require object in your composer.json file.
{
"require": {
"udokmeci/yii2-closest-relation-trait" : "dev-master"
}
}
then run
$> composer update
##Add trait to your base model.
###Via Model Generator
Add following generators array to your configuration file.
$config['modules']['gii'] = [ 'class' => 'yii\gii\Module', 'generators' => [ 'model'=>'udokmeci\yii2closestrelation\model\Generator' ], ];
###Manual
Just add use statement in class and pass the all relation className params from static::getRelationName() method.
Example below:
<?php namespace common\models\base; use Yii; class Page extends \yii\db\ActiveRecord { //here is the trait and see also the relation use \udokmeci\yii2closestrelation\ClosestRelationTrait /** * @inheritdoc */ public static function tableName() { return 'page'; } /** * @return \yii\db\ActiveQuery */ public function getAuthor() { return $this->hasOne(static::getRelationName(Author::className()), ['author_id' => 'id']); } }
Now if you have both \frontend\models\Page and \frontend\models\Author relation returns \frontend\models\Author if you dont have both then \common\models\base\Author is returned again.
Enjoy it.
udokmeci/yii2-closest-relation-trait 适用场景与选型建议
udokmeci/yii2-closest-relation-trait 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 257 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 01 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「relation」 「closest」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 udokmeci/yii2-closest-relation-trait 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 udokmeci/yii2-closest-relation-trait 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 udokmeci/yii2-closest-relation-trait 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A CMS form field for adding has_one relationships using autocomplete
A custom URL rule class for Yii 2 which allows to create translated URL rules
Test easier your Eloquent Models in your Laravel Project
This package introduces the join magic for eloquent models and relations.
Micro Active Record library in PHP, support chain calls, events, and relations.
Relational attributes support for YII2 ActiveRecord
统计信息
- 总下载量: 257
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-01-25