本文实例讲述了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框架的PHP程序设计有所帮助。
更多Yii2中关联查询简单用法示例相关文章请关注PHP中文网!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号