Bug: DataProvider total count is incorrect
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 |
It's broken in 2.0.50. You should downgrade to 2.0.49.4
In dev-master, it works, but there is performance degradation. Proper solution is under discussion here https://github.com/yiisoft/yii2/issues/20213
@sleptor, thanks for the update!
I have downgraded to 2.0.49 for now until the issue is resolved.
please resolved
Thank you for fixing! :)