1.tp下涉及到多个控制器同时都需要取值时则会考虑将取数据的过程放到model层,控制层则会调用model层的方法直接取出相应数据;通过方法的封装可以只采取建立更少的model层来取数据,下面代码展示的就是article模型中通过一个方法取出最新文章、点击量最高文章和推荐文章:
class ArticleModel extends Model {
protected $tableName='article';
function getlist($sign,$length=5){
switch ($sign) {
case 'is_top':
//取出推荐文章
$topArt=M($this->tableName)->where("is_top=1")->field('art_id,title')->order('add_time DESC')->limit($length)->select();
return $topArt;
break;
case 'is_hot':
//取出热门文章
$hotArt=M($this->tableName)->join("blog_category c ON blog_article.cate_id=c.cate_id")->field('art_id,c.cate_id,cate_name,title,read_num')->order('read_num DESC')->limit($length)->select();
return $hotArt;
break;
case 'is_new':
//取出最新数据
$artList=M($this->tableName)->order('art_id DESC')->limit($length)->select();
return $artList;
break;
default:
return '';
break;
}
}
}
2.控制器中调用取出想要的数据:
//取出文章表数据
$artList=M('article')->order('art_id DESC')->limit(5)->select();
//推荐文章
$topArt=D('article')->getlist('is_top',7);
//热门点击
$hotArt=D('article')->getlist('is_hot',7);
原文链接:https://blog.csdn.net/living_ren/article/details/78956466
本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。
还没有人抢沙发呢~