thinkphp6 远程一对一 hansOneThrough 参数解释
发布时间:2021-08-05, 16:31:32 分类:PHP | 编辑 off 网址 | 辅助
正文 2582字数 410,344阅读
TP6源码
/**
* HAS ONE 远程关联定义
* @access public
* @param string $model 模型名
* @param string $through 中间模型名
* @param string $foreignKey 关联外键
* @param string $throughKey 关联外键
* @param string $localKey 当前主键
* @param string $throughPk 中间表主键
* @return HasOneThrough
*/
public function hasOneThrough(string $model, string $through, string $foreignKey = '', string $throughKey = '', string $localKey = '', string $throughPk = ''): HasOneThrough
{
// 记录当前关联信息
$model = $this->parseModel($model);
$through = $this->parseModel($through);
$localKey = $localKey ?: $this->getPk();
$foreignKey = $foreignKey ?: $this->getForeignKey($this->name);
$throughKey = $throughKey ?: $this->getForeignKey((new $through)->getName());
$throughPk = $throughPk ?: (new $through)->getPk();
return new HasOneThrough($this, $model, $through, $foreignKey, $throughKey, $localKey, $throughPk);
}
Run code
Cut to clipboard
hasOneThrough的参数写法白话文解释
public function assessCompany(){
/* 用白话文讲6个参数意思,注意参数3,5配对 参数4,6配对
* 参数1,我最终需要获取的那张表的模型
* 参数2,我需要使用到的中间表模型
* 参数3,中间表与主表相等的字段,填写中间表字段
* 参数4,目标表与中间表相等的字段,填写目标表字段
* 参数5,主表与中间表相等的字段,填写主表字段
* 参数6,中间表与目标表相等的字段,填写中间表字段
* */
return $this->hasOneThrough(CompanyInfo::class,AssessEffect::class,'表2.id','表3.id','表1.assess_effect_id','表2.company_id');
}
Run code
Cut to clipboard
示例:
<?php
namespace app\common\model;
use think\facade\Db;
//优惠券_投放
class CouponPut extends Base
{
protected $pk = 'id';
//CouponPutGoods
public function putGoods()
{
return $this->hasManyThrough(MallGoods::class, CouponPutGoods::class, 'coupon_put_id', 'id', 'id', 'goods_id');
}
//CouponPutStores
public function putStores()
{
return $this->hasManyThrough(Store::class, CouponPutStores::class, 'coupon_put_id', 'id', 'id', 'store_id');
}
//CouponPutRuleList
public function putRule()
{
return $this->hasManyThrough(CouponPutRule::class, CouponPutRuleList::class, 'coupon_put_id', 'id', 'id', 'rule_id');
}
//Coupon
public function putCoupon()
{
return $this->hasManyThrough(Coupon::class, CouponPutList::class, 'coupon_put_id', 'id', 'id', 'coupon_id');
}
}
Run code
Cut to clipboard
————————————————
版权声明:本文为CSDN博主「李小白1」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq449736038/article/details/117281632
版权声明:本文为CSDN博主「李小白1」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq449736038/article/details/117281632
(支付宝)给作者钱财以资鼓励 (微信)→
有过 1 条评论 »
ThinkPHP5有关联模型的操作,
但有部分初学者对数据表中常见的几种表与表的关系还存在着问题,
所以使用不好关联查询。
这里将hasOne、hasMany、belongsTo进行一个详细举例说明。
首先,这3个的大致中文意思:
hasOne:有一个,加上主谓语应该是 ,A 有一个 B hasMany:有很多,A 有很多 B belongsTo:属于, A 属于 B
这里我们准备3张表来理解他们的关系:
user_group 用户分组表:id、title
user 用户表:id、user_group_id、username、password
profile 用户信息表:id、user_id、nickname、sex
1、user表需要关联user_group表,表示每一个 用户 需要知道该用户是 哪个用户分组的;
2、profile表 需要关联 用户表,表示该用户信息数据 是哪个用户的信息;
我们知道一个用户组下面可以有很多用户,
所以:user_group hasMany user;
一个用户 属于 一个用户组,
所以:user belongsTo user_group;
同样是user_group和user表,但我们出发点不同,关系也就不一样了。
每个用户都应该有唯一一条用户信息数据,
所以:user hasOne profile;
一条用户信息 属于 一个用户,
所以:profile belongsTo user
综上:
在User模型中,我们可以定义关联:
function user_group(){ return $this->belongsTo('UserGroup'); } function profile(){ return $this->hasOne('Profile'); }
我们在查询中:
$user = model('User')->find(); $user = $user->user_group; //这样user中就可以包含当前数据对应的user_grou数据了 $user = $user->profile;//这样user中就可以包含当前数据对应的profile数据了
在UserGroup模型中,我们可以定义关联:
function user(){ return $this->hasMany('User'); }
在Profile模型中,我们可以定义关联:
function user(){ return $this->belongsTo('User'); }
注:定义关联function的方法名可以随意定义,一般为表名或模型名;
我们定义的时候是function,但获取时理解为获取属性,所以不加();
很多人理解是我定义了一个profile的方法,
所以应该$user->profile(),这里要特别注意下。
这样我们在查询时,就方便了,不需要使用大量的join。