setPage() public method

Sets the current page number.
public setPage ( integer $value, boolean $validatePage = false )
$value integer the zero-based index of the current page.
$validatePage boolean whether to validate the page number. Note that in order to validate the page number, both [[validatePage]] and this parameter must be true.
示例#1
0
 public function getListByCategory($slug, $fields = [], $with = [], $page = false, $pageSize = false, $order = '')
 {
     $category = Category::getInstance()->getBySlug($slug);
     $model = self::$model->find()->select($fields)->where(['type' => Content::TYPE_ARTICLE, 'status' => Content::STATUS_ACTIVE, 'meta_id' => $category['id']])->join('RIGHT JOIN', Relationship::tableName() . ' relation', 'content_id=id');
     $countModel = clone $model;
     $pagination = new Pagination(['totalCount' => $countModel->count(), 'pageSize' => $pageSize]);
     if ($page) {
         $pagination->setPage($page, true);
     }
     switch (strtoupper($order)) {
         case 'VIEW':
             $model->orderBy('view_total DESC');
             break;
         case 'COMMENT':
             $model->orderBy('comment_total DESC');
             break;
         case 'CREATED':
             $model->orderBy('created_at DESC');
             break;
         case 'UPDATED':
             $model->orderBy('updated_at DESC');
             break;
     }
     return ['data' => $model->with($with)->limit($pagination->getLimit())->offset($pagination->getOffset())->all(), 'pagination' => $pagination];
 }
示例#2
0
 public function getList($fields = [], $with = [], $pageSize = false, $page = false)
 {
     $model = self::$model->find()->where(['type' => Meta::TYPE_ARTICLE_CATEGORIES, 'status' => Meta::STATUS_ACTIVE]);
     $countModel = clone $model;
     $pagination = new Pagination(['totalCount' => $countModel->count('id'), 'pageSize' => $pageSize]);
     if ($page) {
         $pagination->setPage($page, true);
     }
     return ['data' => $model->with($with)->select($fields)->offset($pagination->getOffset())->limit($pagination->getLimit())->asArray()->all(), 'pagination' => $pagination];
 }
示例#3
0
文件: Tag.php 项目: luobenyu/blog-1
 public static function getTags($fields = [], $with = [], $pageSize = false, $page = false)
 {
     $model = Meta::find()->where(['type' => Meta::TYPE_TAG, 'status' => Meta::STATUS_ACTIVE]);
     $countModel = clone $model;
     $pagination = new Pagination(['totalCount' => $countModel->count('id'), 'pageSize' => $pageSize]);
     if ($page) {
         $pagination->setPage($page, true);
     }
     return ['data' => $model->select($fields)->with($with)->limit($pagination->getLimit())->offset($pagination->getOffset())->orderBy('created_at desc')->asArray()->all(), 'pagination' => $pagination];
 }
示例#4
0
 /**
  *
  * @param integer $value
  * @param boolean $validatePage
  * @throws NotFoundHttpException
  */
 public function setPage($value, $validatePage = false)
 {
     parent::setPage($value, $validatePage);
     if ($value < 0 && $this->generateExeption == true) {
         throw new NotFoundHttpException();
     }
     $pageCount = $this->getPageCount();
     if ($value >= $pageCount && $this->generateExeption == true) {
         throw new NotFoundHttpException();
     }
     if (Yii::$app->getRequest()->get($this->pageParam) == 1 && $this->generateExeption == true) {
         throw new NotFoundHttpException();
     }
 }
示例#5
0
 public function testFirstLastPageLabels()
 {
     $pagination = new Pagination();
     $pagination->setPage(5);
     $pagination->totalCount = 500;
     $pagination->route = 'test';
     $output = LinkPager::widget(['pagination' => $pagination, 'firstPageLabel' => true, 'lastPageLabel' => true]);
     static::assertContains('<li class="first"><a href="/?r=test&amp;page=1" data-page="0">1</a></li>', $output);
     static::assertContains('<li class="last"><a href="/?r=test&amp;page=25" data-page="24">25</a></li>', $output);
     $output = LinkPager::widget(['pagination' => $pagination, 'firstPageLabel' => 'First', 'lastPageLabel' => 'Last']);
     static::assertContains('<li class="first"><a href="/?r=test&amp;page=1" data-page="0">First</a></li>', $output);
     static::assertContains('<li class="last"><a href="/?r=test&amp;page=25" data-page="24">Last</a></li>', $output);
     $output = LinkPager::widget(['pagination' => $pagination, 'firstPageLabel' => false, 'lastPageLabel' => false]);
     static::assertNotContains('<li class="first">', $output);
     static::assertNotContains('<li class="last">', $output);
 }
 /**
  * List all topics
  */
 public function actionIndex()
 {
     $pageNo = Yii::$app->request->get("page", 0) - 1;
     $sectionId = Yii::$app->request->get("sectionId", Yii::$app->request->post("sectionId", ''));
     $sectionList = $this->getSectionList();
     $query = Topic::find();
     if ($sectionId) {
         $query->andFilterWhere(['SectionId' => $sectionId]);
     }
     // config pagination properties
     $countQuery = clone $query;
     $pages = new Pagination(['totalCount' => $countQuery->count()]);
     $pages->pageSize = Yii::$app->getModule('helpsystem')->getModule('admin')->topicPageLimit;
     $pages->params = ["sectionId" => $sectionId];
     $offset = $pages->pageSize * $pageNo;
     if ($countQuery->count() < $offset) {
         $offset = floor($countQuery->count() / $pages->pageSize) + 1;
     }
     $topicList = $query->offset($offset)->limit($pages->pageSize)->orderBy('Order ASC')->all();
     $pages->setPage($pageNo);
     return $this->render("index", ['topicList' => $topicList, 'sectionList' => $sectionList, 'sectionId' => $sectionId, 'pages' => $pages, 'pageNo' => $pageNo + 1]);
 }
示例#7
0
 public static function getPagedRows($query, $config = [])
 {
     $db = isset($config['db']) ? $config['db'] : null;
     $cloneQuery = clone $query;
     $pager = new Pagination(['totalCount' => $cloneQuery->count('*', $db)]);
     if (isset($config['page'])) {
         $pager->setPage($config['page'], true);
     }
     if (isset($config['pageSize'])) {
         $pager->setPageSize($config['pageSize'], true);
     }
     $rows = $query->offset($pager->offset)->limit($pager->limit);
     if (isset($config['orderBy'])) {
         $rows = $rows->orderBy($config['orderBy']);
     }
     $rows = $rows->all($db);
     $rowsLable = isset($config['rows']) ? $config['rows'] : 'rows';
     $pagerLable = isset($config['pager']) ? $config['pager'] : 'pager';
     $ret = [];
     $ret[$rowsLable] = $rows;
     $ret[$pagerLable] = $pager;
     return $ret;
 }
 /**
  * 获取列表记录
  *
  * @param array|string $condition 条件
  * @param array $params 参数
  * @param string $order 排序
  * @param int $page 页码
  * @param int $pageSize 每页数量
  * @return array
  */
 public static function getListByCondition($condition = '', $params = [], $order = null, $page = 1, $pageSize = 20)
 {
     $pagination = new Pagination();
     $pagination->setPage($page);
     $pagination->setPageSize($pageSize);
     $pagination->totalCount = static::find()->innerJoinWith('menuUrl')->andWhere($condition, $params)->count(1);
     $list = static::find()->innerJoinWith('menuUrl')->andWhere($condition, $params)->offset($pagination->getOffset() - $pagination->getPageSize())->limit($pagination->getPageSize())->orderBy($order)->asArray()->all();
     return ['paginationObj' => $pagination, 'pagination' => ['currentPage' => $page, 'pageSize' => $pageSize, 'pageCount' => $pagination->getPageCount(), 'totalCount' => $pagination->totalCount], 'list' => $list];
 }
示例#9
0
 /**
  * @inheritdoc
  * @see yii\data\Pagination::setPage()
  */
 public function setPage($value, $validatePage = false)
 {
     parent::setPage($value);
     $this->_page = parent::getPage();
 }
示例#10
0
 public static function getPagedRows($query, $config = [])
 {
     $countQuery = clone $query;
     $pages = new Pagination(['totalCount' => $countQuery->count()]);
     if (isset($config['page'])) {
         $pages->setPage($config['page'], true);
     }
     if (isset($config['pageSize'])) {
         $pages->setPageSize($config['pageSize'], true);
     }
     $rows = $query->offset($pages->offset)->limit($pages->limit);
     if (isset($config['order'])) {
         $rows = $rows->orderBy($config['order']);
     }
     $rows = $rows->all();
     $rowsLable = 'rows';
     $pagesLable = 'pages';
     if (isset($config['rows'])) {
         $rowsLable = $config['rows'];
     }
     if (isset($config['pages'])) {
         $pagesLable = $config['pages'];
     }
     $ret = [];
     $ret[$rowsLable] = $rows;
     $ret[$pagesLable] = $pages;
     return $ret;
 }
示例#11
0
 public function findAllWithPage($para = [], $obj = false)
 {
     self::paraProcess($para);
     if (empty($this->_relations)) {
         $feedback = !empty(self::$_params[XIIS_PARAMS_CONDITION]) ? parent::find()->select(self::$_params[XIIS_PARAMS_FIELDS])->where(self::$_params[XIIS_PARAMS_CONDITION])->orderBy(self::$_params[XIIS_PARAMS_ORDERBY]) : parent::find()->select(self::$_params[XIIS_PARAMS_FIELDS])->orderBy(self::$_params[XIIS_PARAMS_ORDERBY]);
     } else {
         $feedback = !empty(self::$_params[XIIS_PARAMS_CONDITION]) ? parent::find()->select(self::$_params[XIIS_PARAMS_FIELDS])->where(self::$_params[XIIS_PARAMS_CONDITION])->orderBy(self::$_params[XIIS_PARAMS_ORDERBY])->with($this->_relations) : parent::find()->select(self::$_params[XIIS_PARAMS_FIELDS])->orderBy(self::$_params[XIIS_PARAMS_ORDERBY])->with($this->_relations);
     }
     if ($feedback) {
         $countQuery = clone $feedback;
         $pages = new Pagination([XIIS_FEEDBACK_COUNT => $countQuery->count()]);
         $pages->setPage(self::$_params[XIIS_PARAMS_PAGE] - 1);
         $pages->setPageSize(self::$_params[XIIS_PARAMS_PAGESIZE]);
         if ($obj) {
             $models = $feedback->offset($pages->offset)->limit($pages->limit);
         } else {
             $models = $feedback->offset($pages->offset)->limit($pages->limit)->asArray();
         }
         $commandQuery = clone $models;
         self::logRecord(['sql' => __FUNCTION__ . ' : ' . $commandQuery->createCommand()->getRawSql()]);
         $data[XIIS_FEEDBACK_DATA] = $models->all();
         $data[XIIS_FEEDBACK_PAGER] = self::getPager($pages);
         if ($models) {
             return self::getResponse(EC_ARPLUS_READ_DATA_SUCCESS, $data);
         } else {
             return self::getResponse(EC_ARPLUS_READ_FAIL_NO_DATA);
         }
     } else {
         return self::getResponse(EC_ARPLUS_READ_FAIL_NO_DATA, $feedback);
     }
 }
示例#12
0
 /**
  * 刷新所有用户基本信息
  *
  * @since 0.0.1
  * @param {integer} [$page=1] 页码
  * @return {boolean}
  * @example \Yii::$app->wechat->refreshUsers($page);
  */
 public function refreshUsers($page = 1)
 {
     $query = WechatUser::find()->where(['appid' => $this->app->appid])->select('openid');
     $pageSize = 100;
     $pagination = new Pagination(['totalCount' => $query->count(), 'defaultPageSize' => $pageSize, 'pageSizeLimit' => [0, $pageSize]]);
     $pagination->setPage($page - 1, true);
     $users = $query->offset($pagination->offset)->limit($pagination->limit)->asArray()->all();
     $user_list = [];
     foreach ($users as $user) {
         $user['lang'] = \Yii::$app->language;
         $user_list['user_list'][] = $user;
     }
     if ($user_list) {
         $data = $this->getData('/cgi-bin/user/info/batchget', ['access_token' => $this->getAccessToken()], Json::encode($user_list));
         if (isset($data['user_info_list'])) {
             foreach ($data['user_info_list'] as $_user) {
                 $user = WechatUser::findOne(['appid' => $this->app->appid, 'openid' => $_user['openid']]);
                 if (!$user) {
                     $user = new WechatUser();
                     $user->appid = $this->app->appid;
                     $user->openid = $_user['openid'];
                 }
                 $user->subscribe = $_user['subscribe'];
                 if ($user->subscribe == 1) {
                     $user->subscribed_at = $_user['subscribed_at'];
                     $user->name = $_user['nickname'];
                     $user->sex = $_user['sex'];
                     $user->country = $_user['country'];
                     $user->city = $_user['city'];
                     $user->province = $_user['province'];
                     $user->language = $_user['language'];
                     $user->headimgurl = $_user['headimgurl'];
                     $user->remark = $_user['remark'];
                     $user->groupid = $_user['groupid'];
                 }
                 if (isset($_user['unionid'])) {
                     $user->unionid = $_user['unionid'];
                 }
                 $user->save();
             }
         }
     }
     return $page < $pagination->pageCount ? $this->refreshUsers($page + 1) : $this->errcode == 0;
 }
示例#13
0
 public static function findAllWithPage($para = [], $obj = false)
 {
     self::paraProcess($para);
     $feedback = !empty(self::$_paraCondition) ? parent::find()->select(self::$_paraSelectFields)->where(self::$_paraCondition)->orderBy(self::$_paraOrderby) : parent::find()->select(self::$_paraSelectFields)->orderBy(self::$_paraOrderby);
     if ($feedback) {
         $countQuery = clone $feedback;
         $pages = new Pagination(['totalCount' => $countQuery->count()]);
         $pages->setPage(self::$_paraPage - 1);
         $pages->setPageSize(self::$_paraLimit);
         if ($obj) {
             $models = $feedback->offset($pages->offset)->limit($pages->limit)->all();
         } else {
             $models = $feedback->offset($pages->offset)->limit($pages->limit)->asArray()->all();
         }
         $data['data'] = $models;
         if (self::$_pageLinkPagerOn) {
             $data['pager'] = LinkPager::widget(['pagination' => $pages]);
         } else {
             $data['pager'] = ['defaultPageSize' => $pages->defaultPageSize, 'forcePageParam' => $pages->forcePageParam, 'limit' => $pages->limit, 'links' => $pages->links, 'offset' => $pages->offset, 'page' => $pages->page, 'pageCount' => $pages->pageCount, 'pageParam' => $pages->pageParam, 'pageSize' => $pages->pageSize, 'pageSizeLimit' => $pages->pageSizeLimit, 'pageSizeParam' => $pages->pageSizeParam, 'params' => $pages->params, 'route' => $pages->route, 'totalCount' => $pages->totalCount, 'urlManager' => $pages->urlManager, 'validatePage' => $pages->validatePage];
         }
         return self::getResponse(self::XII_READ_DATA_SUCCESS, $data);
     } else {
         return self::getResponse(self::XII_READ_FAIL_NO_DATA, $feedback);
     }
 }
示例#14
-1
 public function testValidatePage()
 {
     $pagination = new Pagination();
     $pagination->validatePage = true;
     $pagination->pageSize = 10;
     $pagination->totalCount = 100;
     $pagination->setPage(999, true);
     $this->assertEquals(9, $pagination->getPage());
     $pagination->setPage(999, false);
     $this->assertEquals(999, $pagination->getPage());
 }