Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: DataProvider total count is incorrect #20215

Closed
srdjan92 opened this issue Jun 25, 2024 · 4 comments
Closed

Bug: DataProvider total count is incorrect #20215

srdjan92 opened this issue Jun 25, 2024 · 4 comments
Milestone

Comments

@srdjan92
Copy link

srdjan92 commented Jun 25, 2024

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 90c0eb0

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
@srdjan92 srdjan92 changed the title Bug: Pagination total count is incorrect Bug: DataProvider total count is incorrect Jun 25, 2024
@sleptor
Copy link

sleptor commented Jun 25, 2024

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 #20213

@srdjan92
Copy link
Author

@sleptor, thanks for the update!

I have downgraded to 2.0.49 for now until the issue is resolved.

@xuweiguo
Copy link

please resolved

@samdark samdark added this to the 2.0.51 milestone Jun 30, 2024
@samdark samdark closed this as completed Jul 12, 2024
@arievanden
Copy link

Thank you for fixing! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants