yii\data\Pagination::setPage PHP Method

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.
    public function setPage($value, $validatePage = false)
    {
        if ($value === null) {
            $this->_page = null;
        } else {
            $value = (int) $value;
            if ($validatePage && $this->validatePage) {
                $pageCount = $this->getPageCount();
                if ($value >= $pageCount) {
                    $value = $pageCount - 1;
                }
            }
            if ($value < 0) {
                $value = 0;
            }
            $this->_page = $value;
        }
    }

Usage Example

示例#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];
 }
All Usage Examples Of yii\data\Pagination::setPage