最新消息: 新版网站上线了!!!

yii2 利用yiidbQuery实现分页

public function actionJoin() {

        $query = (new \yii\db\Query())
            ->select('*')
            ->from('{{%news_category}} AS a')
            ->leftJoin('{{%news}} AS t','t.cat_id = a.id')
           // ->where(['c.name'=>'新闻分类'])
            ;
        $pages = new \yii\data\Pagination(['totalCount' => $query->count(),'pageSize'=>1]);
        $pages->defaultPageSize = 30; //设置分页数目的大小,默认是20
        $models = $query->offset($pages->offset)
            ->limit($pages->limit)
            ->orderBy('a.id DESC')
            ->all();
        return $this->render('join', [
            'models' => $models,
            'pages' => $pages,

        ]);
        /*
        /* view.php */
         * foreach ($models as $model) {
                // 在这里显示 $model,比如:
                echo $model['goods_name']."<br/>";
            }

            // display pagination
            echo \yii\widgets\LinkPager::widget([
                'pagination' => $pages,

            ]);
         */

       }

转载请注明:谷谷点程序 » yii2 利用yiidbQuery实现分页