YII2 joinWith的使用实例
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
还没有评论,来说两句吧...