1.首先要有两个模型
投票劵表(vote_conpon_type) 模型为 VoteCouponType
投票券子表(vote_coupon) 模型为 VoteCoupon
模型VoteCouponType必填内容:
public function getVoteCoupon() { return $this->hasMany(VoteCoupon::className(), ['coupon_type' => 'id']); }
模型VoteCoupon必填内容:
public function getVoteCouponType() { return $this->hasOne(VoteCouponType::className(), ['id' => 'coupon_type']); }
使用:
$model = VoteCouponType::find() ->joinWith('voteCoupon') ->orderBy('vote_coupon.coupon_type, vote_coupon_type.id') ->where(['usercenter.vote_coupon_type.appid'=>5]);
注意以下几点:
1.当表名存在下划线时,joinWith里的表名首字母要小写,下划线结束后的第一个字母要大写
2.有where条件时,字段名前的表名,有些跨数据库的,数据库名要写全。