YII2 joinWith的使用实例

女爷i 2022-02-19 08:49 335阅读 0赞

1.首先要有两个模型

  1. 投票劵表(vote\_conpon\_type 模型为 VoteCouponType
  2. 投票券子表(vote\_coupon 模型为 VoteCoupon

模型VoteCouponType必填内容:

  1. public function getVoteCoupon()
  2. {
  3. return $this->hasMany(VoteCoupon::className(), ['coupon_type' => 'id']);
  4. }

模型VoteCoupon必填内容:

  1. public function getVoteCouponType()
  2. {
  3. return $this->hasOne(VoteCouponType::className(), ['id' => 'coupon_type']);
  4. }

使用:

  1. $model = VoteCouponType::find()
  2. ->joinWith('voteCoupon')
  3. ->orderBy('vote_coupon.coupon_type, vote_coupon_type.id')
  4. ->where(['usercenter.vote_coupon_type.appid'=>5]);

注意以下几点:

1.当表名存在下划线时,joinWith里的表名首字母要小写,下划线结束后的第一个字母要大写

2.有where条件时,字段名前的表名,有些跨数据库的,数据库名要写全。

参考:https://blog.csdn.net/qq_37921425/article/details/83420817

发表评论

表情:
评论列表 (有 0 条评论,335人围观)

还没有评论,来说两句吧...

相关阅读