ThinkPHP中查询获取随机数据,随机文章列表

2021-06-10 15:1463

ThinkPHP中查询获取随机数据,随机文章列表

ThinkPHP 5.1:

->orderRand()
 //实现的hi_rand_article钩子方法
    public function hiRandArticle($param)
    {
        $config = $this->getConfig();
        
        $field = !empty($param['field']) ? $param['field'] : 'post.*,min(category_post.category_id) as category_id';
        $join = [
            ['__PORTAL_CATEGORY_POST__ category_post', 'post.id = category_post.post_id'],
        ];
        $where = [
            'post.post_status' => 1,
            'post.post_type'   => 1,
            'post.delete_time' => 0
        ];
        
        $portalPostModel = new PortalPostModel();
        $articles = $portalPostModel->alias('post')->field($field)
            ->join($join)
            ->limit($config['hi_limit'])
            ->where($where)
            ->where('post.published_time', ['> time', 0], ['<', time()], 'and')
            ->orderRand()//orderRand()不需要参数
            ->group('post.id')
            ->select();
        
        $this->assign('list',$articles);
        echo $this->fetch('widget');
    }


ThinkPHP 6.0:

->orderRaw("rand() , id DESC")


上一篇: ThinkPHP5.1的Log::write()