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條件時,字段名前的表名,有些跨數據庫的,數據庫名要寫全。
參考:https://blog.csdn.net/qq_37921425/article/details/83420817