M
M
marsdenden2020-05-13 08:37:06
Yii
marsdenden, 2020-05-13 08:37:06

Yii2 how to disable pagination or increase the limit?

Pagination is a must. Sometimes. Not everyone.
I can't figure out how to turn it off. The controller code is the simplest from the documentation

class RestActiveController extends ActiveController
{
    public $serializer = [
        'class' => 'yii\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];
}


I'm making a service that will work in LAN and at once pick up a response of 60,000 records from the server a dozen megabytes (unzipped) from the server - no problem. The problem is pagination. Having climbed in the source code, rummaged through the open spaces of Google, having tested /goods?per-page=60000, I received a brazen statement from the server side that the maximum per-page is 50. I can’t make 1200 requests to get 60000 records, how can I get around this?

PS. Removed the serializer - still pagination remains, and still the limit is 50 records

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim, 2020-05-13
@marsdenden

You can set the page size:
/goods?per-page=1000
When Query /goodsRuns yii\rest\IndexAction, which returns

ActiveDataProvider(['query' => $modelClass::find()])

Those. the Pagination object is default there
$pageParam = 'page'
$pageSizeParam = 'per-page' 
$pageSizeLimit = [1, 50]
And the default yii\data\Paginationone tries to take the parameters from $request->getQueryParams().
Your task is to override the pagination limit of the paginator via DI PS: 60000 will be too much. Even such a powerful service as VKontakte API has a maximum of 1000 by default 100. I would also recommend doing just that. $pageSizeLimit = [1, 1000]

I
Ivan Ivanov, 2020-05-13
@maksim_fix

Googling your question, I immediately found the answer.

public function actionIndex()
{
    //$posts = Post::find()->all();
    $query = Post::find();
    $pages = new Pagination(['totalCount' => $query->count()]);
    $posts = $query->offset($pages->offset)
        ->limit($pages->limit)
        ->all();
    return $this->render('index', compact('posts', 'pages'));
}

specify the limit you need and that's it. Is it really that hard to google it?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question