本文实例讲述了yii2中关联查询用法。分享给大家供大家参考,具体如下:
有两张表,post和category,post.cate_id对应category.id
使用gii上升这两张表的model
然后post的model中有如下代码
public function getcate()
{
return $this->hasone(category::classname(), ['id' => 'cate_id']);
}
在post这个model最下面在添加如下方法即可获取关联表内容
public static function getpostsbycategory($cate_id)
{
return post::find()
->joinwith('cate')
->where(['post.cate_id'=>$cate_id])
->asarray()
->all();
}
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!