工学1号馆

home

tp5.1中使用模型在volist标签显示多个数据表中的数据

Wu Yudong    October 25, 2018     ThinkPHP   671   

今天在使用tp5.1开发的时候,遇到一个问题:使用模型在三个数据表中联合查询,然后使用volist在模板中显示数据,手册上关于模型的讲解有限,我这里记录一下:

3个表分别是用户表(user)、课程表(course)、课程收藏表(user_favorites)

根据用户id得到用户收藏的课程信息

第一步:从user_favorites表中得到课程id

第二步:根据课程id从course表中得到课程信息(其中包括教师id)

第三步:根据教师id号从user表中得到教师姓名

代码如下:

public function collect(){
	$userid = get_current_userid();
	$courseIdArray = UserFavoritesModel::where('uid', $userid)->column('object_id');
	$courselist = CourseModel::where('id','IN', $courseIdArray)->select();
	foreach ($courselist as $course) {
		$course['nickname'] = UsersModel::where('id', $course['cs_teacher'])->find()->nickname;
	}
	$this->assign("courselist",$courselist);
	return $this->fetch('collect');
}

这里的思路比较复杂,因为我也想不出简洁的办法,留着以后对模型操作熟悉了以后再做一次

如果文章对您有帮助,欢迎点击下方按钮打赏作者

Comments

No comments yet.
To verify that you are human, please fill in "七"(required)