正文 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