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 fix #20055 causes bad result for pagination using for example gridview #20175

Closed
procin opened this issue May 30, 2024 · 7 comments
Closed
Milestone

Comments

@procin
Copy link

procin commented May 30, 2024

What steps will reproduce the problem?

When creating the dataprovider, for example:
$provider = new ActiveDataProvider([
'query' => ...,
'pagination' => [...]
]);

Setting the 'pagination' calls the setPagination() function, which now directly calls the getTotalCount() function, which calls the prepareTotalCount() function.
Now the value for $_totalCount will be queried from the database.

When using a GridView with a SearchModel, the search parameters for the query are loaded in a later stage.
The search parameters can cause for a different number of rows to be returned.

The prepareTotalCount() is not called again, because $_totalCount is already set.

The pagination bar now works with a completely wrong count value. (for example, displays more pages than there actually are)

Calling prepareTotalCount() again is not a solution, the count on a big table can be very expensive.

Additional info

Q A
Yii version 2.0.50
@samdark samdark added this to the 2.0.50.1 milestone May 31, 2024
@samdark
Copy link
Member

samdark commented May 31, 2024

#20055 @lav45 would you please take a look?

lav45 added a commit to lav45/yii2 that referenced this issue Jun 1, 2024
…od executable. Add internal cache for ActiveDataProvider and SqlDataProvider.
@nadar
Copy link
Contributor

nadar commented Jun 1, 2024

@samdark it seems since 2.0.50 the active data provider is broken when using pagination and/or sort params:

yii\base\InvalidConfigException: The "query" property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses. in /var/www/html/vendor/yiisoft/yii2/data/ActiveDataProvider.php:164
Stack trace:
#0 /var/www/html/vendor/yiisoft/yii2/data/BaseDataProvider.php(171): yii\data\ActiveDataProvider->prepareTotalCount()
#1 /var/www/html/vendor/yiisoft/yii2/data/BaseDataProvider.php(222): yii\data\BaseDataProvider->getTotalCount()
#2 /var/www/html/vendor/yiisoft/yii2/base/Component.php(180): yii\data\BaseDataProvider->setPagination()
#3 /var/www/html/vendor/yiisoft/yii2/BaseYii.php(557): yii\base\Component->__set()
#4 /var/www/html/vendor/yiisoft/yii2/base/BaseObject.php(107): yii\BaseYii::configure()

this code breaks with the above stack trace in version 2.0.50

return new ActiveDataProvider([
            'pagination' => [
                'defaultPageSize' => 75,
            ],
            'query' => $query
                ->select([SchemaRow::tableName().'.id', 'title', 'subtitle', 'is_visible'])
                ->byLikeTitle($model->query)
                ->isNotTrashed()
                ->andFilterWhere([SchemaRow::tableName().'.id' => $model->ids])
                ->bySchemaId($id),
            'sort' => [
                'defaultOrder' => [
                    'title' => SORT_ASC,
                ],
            ],
        ]);

removing the sort/and or pagination definition and it works again. We had to downgrad to 2.0.49 in order to get the app working.

change the code to:

return new ActiveDataProvider([
            'query' => $query
                ->select([SchemaRow::tableName().'.id', 'title', 'subtitle', 'is_visible'])
                ->byLikeTitle($model->query)
                ->isNotTrashed()
                ->andFilterWhere([SchemaRow::tableName().'.id' => $model->ids])
                ->bySchemaId($id),
        ]);

for testing, and it works also in 2.0.50

php 8.3.4

@nadar
Copy link
Contributor

nadar commented Jun 1, 2024

@samdark not sure its related to be honest. but i also have problems with the active data provider with latest version. Let me know if i should create a fresh issue.

@lav45
Copy link
Contributor

lav45 commented Jun 1, 2024

@nadar fixed in to the PR #20176

@SamMousa
Copy link
Contributor

SamMousa commented Jun 3, 2024

Workaround is to make sure the configuration array contains query before pagination. That way query is set before pagination is set.

@nadar
Copy link
Contributor

nadar commented Jun 8, 2024

i tested the fix in dev-master, seems to work. thanks 👍

@samdark
Copy link
Member

samdark commented Jun 10, 2024

Thanks for verifying it.

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