您的位置:首页 > 编程语言 > PHP开发

Yii2关联查询用法

2015-06-08 00:46 639 查看
有两张表,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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: