首页 > 开发 > PHP > 正文

Yii2中关联查询简单用法示例

2024-05-04 22:28:46
字体:
来源:转载
供稿:网友

本文实例讲述了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();}

更多关于Yii相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表