I’m having issues with the pageSizeLimit
parameter in the ActiveDataProvider.
Setting this option in the pagination attribute, the total count returned is the count of ALL items in the queried table.
Via query param I try to filter a list of clients according with the route ID column, and in the search model I set the page size limit as the following:
$query = Client::find()->indexBy("id");
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSizeLimit' => [1, 100]
]
]);
$query->andFilterWhere([
'id' => $this->id,
'status' => !$this->status || $this->status == '' ? [Client::STATUS_ACTIVE, Client::STATUS_BLOCKED, Client::STATUS_PENDING_UPDATE] : $this->status,
'zona_id' => $this->zona_id,
]);
...
The pageSizeLimit will build the following SQL sentence:
SELECT COUNT(*) FROM `client`;
But if I comment/delete the pagination
attribute configuration, then the total count will be the actual total count and the SQL count will be built correctly:
SELECT COUNT(*) FROM `client` where (`client`.`status` IN (1, 2, 0)) AND (`client`.`route_id`='1') LIMIT 20;
Is a “bug” in the pagination or maybe I’m having a mistake I’m not seeing?
opened 01:48PM - 25 Jun 24 UTC
closed 05:49AM - 12 Jul 24 UTC
<!--
Please use this issue tracker for bugs and feature requests only. In case … you need support, please use one of
Yii communities listed at https://github.com/yiisoft/yii2/wiki/communities
-->
I am still not sure if this is a bug, but the Yii2 search used to work fine with this approach for years. In the application I am working on, we used the following approach, but after the upgrade to version 2.0.50, the total count in the data provider object is not correct anymore.
### What steps will reproduce the problem?
```
public function search() {
$query = User::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 20,
'page' =>!Yii::$app->request->isConsoleRequest ? (int) Yii::$app->request->getQueryParam('page', 0) : 0,
]
]);
$this->load($params);
if (!$this->validate()) {
$query->andWhere("1=0");
return $dataProvider;
}
//Apply additional filters to the query
return $dataProvider;
}
```
### What is the expected result?
I expect the correct total count value to be calculated with applied query filters.
### What do you get instead?
I got a total count of all records in the table without query filters applied to the query.
### Additional info
In the example above, we pass the pagination configuration when the `ActiveDataProvider` instance is created. The pagination object will be created immediately, and the total count in the `Pagination` object will also be populated using the `$query` passed at that point.
The issue will not be present if we do not pass the pagination configuration to the data provider.
As far as I see, this problem was caused by the following commit https://github.com/yiisoft/yii2/commit/90c0eb02d1768e7f2ab3512e1da5b8475dc5d32d
I know how to work around the current issue, but I wanted to bring up this topic because I am afraid it will affect many applications.
| Q | A
| ---------------- | ---
| Yii version | 2.0.50
| PHP version | 8.2
| Operating system | Ubuntu 22
I didn’t see this issue in Github. I have updated and now is working again.