From 0e5219aecb89e3b2341f5fb1f2ce38c425b7c150 Mon Sep 17 00:00:00 2001 From: Viktor Khokhryakov Date: Wed, 15 Jan 2025 16:19:27 +0400 Subject: [PATCH] Fix #20239: fix `yii\data\ActiveDataProvider` to avoid unexpected pagination results with UNION queries --- composer.lock | 130 +++++++++--------- framework/CHANGELOG.md | 1 + framework/data/ActiveDataProvider.php | 39 ++++-- .../framework/data/ActiveDataProviderTest.php | 23 ++++ 4 files changed, 120 insertions(+), 73 deletions(-) diff --git a/composer.lock b/composer.lock index f14bc829821..1494c2877a9 100644 --- a/composer.lock +++ b/composer.lock @@ -147,20 +147,20 @@ }, { "name": "ezyang/htmlpurifier", - "version": "v4.17.0", + "version": "v4.18.0", "source": { "type": "git", "url": "https://github.com/ezyang/htmlpurifier.git", - "reference": "bbc513d79acf6691fa9cf10f192c90dd2957f18c" + "reference": "cb56001e54359df7ae76dc522d08845dc741621b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/ezyang/htmlpurifier/zipball/bbc513d79acf6691fa9cf10f192c90dd2957f18c", - "reference": "bbc513d79acf6691fa9cf10f192c90dd2957f18c", + "url": "https://api.github.com/repos/ezyang/htmlpurifier/zipball/cb56001e54359df7ae76dc522d08845dc741621b", + "reference": "cb56001e54359df7ae76dc522d08845dc741621b", "shasum": "" }, "require": { - "php": "~5.6.0 || ~7.0.0 || ~7.1.0 || ~7.2.0 || ~7.3.0 || ~7.4.0 || ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0" + "php": "~5.6.0 || ~7.0.0 || ~7.1.0 || ~7.2.0 || ~7.3.0 || ~7.4.0 || ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0" }, "require-dev": { "cerdic/css-tidy": "^1.7 || ^2.0", @@ -202,9 +202,9 @@ ], "support": { "issues": "https://github.com/ezyang/htmlpurifier/issues", - "source": "https://github.com/ezyang/htmlpurifier/tree/v4.17.0" + "source": "https://github.com/ezyang/htmlpurifier/tree/v4.18.0" }, - "time": "2023-11-17T15:01:25+00:00" + "time": "2024-11-01T03:51:45+00:00" }, { "name": "yiisoft/yii2-composer", @@ -515,16 +515,16 @@ }, { "name": "myclabs/deep-copy", - "version": "1.12.0", + "version": "1.12.1", "source": { "type": "git", "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c" + "reference": "123267b2c49fbf30d78a7b2d333f6be754b94845" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c", - "reference": "3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/123267b2c49fbf30d78a7b2d333f6be754b94845", + "reference": "123267b2c49fbf30d78a7b2d333f6be754b94845", "shasum": "" }, "require": { @@ -563,7 +563,7 @@ ], "support": { "issues": "https://github.com/myclabs/DeepCopy/issues", - "source": "https://github.com/myclabs/DeepCopy/tree/1.12.0" + "source": "https://github.com/myclabs/DeepCopy/tree/1.12.1" }, "funding": [ { @@ -571,20 +571,20 @@ "type": "tidelift" } ], - "time": "2024-06-12T14:39:25+00:00" + "time": "2024-11-08T17:47:46+00:00" }, { "name": "nikic/php-parser", - "version": "v5.1.0", + "version": "v5.4.0", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "683130c2ff8c2739f4822ff7ac5c873ec529abd1" + "reference": "447a020a1f875a434d62f2a401f53b82a396e494" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/683130c2ff8c2739f4822ff7ac5c873ec529abd1", - "reference": "683130c2ff8c2739f4822ff7ac5c873ec529abd1", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/447a020a1f875a434d62f2a401f53b82a396e494", + "reference": "447a020a1f875a434d62f2a401f53b82a396e494", "shasum": "" }, "require": { @@ -627,9 +627,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v5.1.0" + "source": "https://github.com/nikic/PHP-Parser/tree/v5.4.0" }, - "time": "2024-07-01T20:03:41+00:00" + "time": "2024-12-30T11:07:19+00:00" }, { "name": "phar-io/manifest", @@ -751,35 +751,35 @@ }, { "name": "phpunit/php-code-coverage", - "version": "9.2.31", + "version": "9.2.32", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "48c34b5d8d983006bd2adc2d0de92963b9155965" + "reference": "85402a822d1ecf1db1096959413d35e1c37cf1a5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/48c34b5d8d983006bd2adc2d0de92963b9155965", - "reference": "48c34b5d8d983006bd2adc2d0de92963b9155965", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/85402a822d1ecf1db1096959413d35e1c37cf1a5", + "reference": "85402a822d1ecf1db1096959413d35e1c37cf1a5", "shasum": "" }, "require": { "ext-dom": "*", "ext-libxml": "*", "ext-xmlwriter": "*", - "nikic/php-parser": "^4.18 || ^5.0", + "nikic/php-parser": "^4.19.1 || ^5.1.0", "php": ">=7.3", - "phpunit/php-file-iterator": "^3.0.3", - "phpunit/php-text-template": "^2.0.2", - "sebastian/code-unit-reverse-lookup": "^2.0.2", - "sebastian/complexity": "^2.0", - "sebastian/environment": "^5.1.2", - "sebastian/lines-of-code": "^1.0.3", - "sebastian/version": "^3.0.1", - "theseer/tokenizer": "^1.2.0" + "phpunit/php-file-iterator": "^3.0.6", + "phpunit/php-text-template": "^2.0.4", + "sebastian/code-unit-reverse-lookup": "^2.0.3", + "sebastian/complexity": "^2.0.3", + "sebastian/environment": "^5.1.5", + "sebastian/lines-of-code": "^1.0.4", + "sebastian/version": "^3.0.2", + "theseer/tokenizer": "^1.2.3" }, "require-dev": { - "phpunit/phpunit": "^9.3" + "phpunit/phpunit": "^9.6" }, "suggest": { "ext-pcov": "PHP extension that provides line coverage", @@ -788,7 +788,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "9.2-dev" + "dev-main": "9.2.x-dev" } }, "autoload": { @@ -817,7 +817,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", "security": "https://github.com/sebastianbergmann/php-code-coverage/security/policy", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.31" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.32" }, "funding": [ { @@ -825,7 +825,7 @@ "type": "github" } ], - "time": "2024-03-02T06:37:42+00:00" + "time": "2024-08-22T04:23:01+00:00" }, { "name": "phpunit/php-file-iterator", @@ -1070,45 +1070,45 @@ }, { "name": "phpunit/phpunit", - "version": "9.6.19", + "version": "9.6.22", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "a1a54a473501ef4cdeaae4e06891674114d79db8" + "reference": "f80235cb4d3caa59ae09be3adf1ded27521d1a9c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/a1a54a473501ef4cdeaae4e06891674114d79db8", - "reference": "a1a54a473501ef4cdeaae4e06891674114d79db8", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/f80235cb4d3caa59ae09be3adf1ded27521d1a9c", + "reference": "f80235cb4d3caa59ae09be3adf1ded27521d1a9c", "shasum": "" }, "require": { - "doctrine/instantiator": "^1.3.1 || ^2", + "doctrine/instantiator": "^1.5.0 || ^2", "ext-dom": "*", "ext-json": "*", "ext-libxml": "*", "ext-mbstring": "*", "ext-xml": "*", "ext-xmlwriter": "*", - "myclabs/deep-copy": "^1.10.1", - "phar-io/manifest": "^2.0.3", - "phar-io/version": "^3.0.2", + "myclabs/deep-copy": "^1.12.1", + "phar-io/manifest": "^2.0.4", + "phar-io/version": "^3.2.1", "php": ">=7.3", - "phpunit/php-code-coverage": "^9.2.28", - "phpunit/php-file-iterator": "^3.0.5", + "phpunit/php-code-coverage": "^9.2.32", + "phpunit/php-file-iterator": "^3.0.6", "phpunit/php-invoker": "^3.1.1", - "phpunit/php-text-template": "^2.0.3", - "phpunit/php-timer": "^5.0.2", - "sebastian/cli-parser": "^1.0.1", - "sebastian/code-unit": "^1.0.6", + "phpunit/php-text-template": "^2.0.4", + "phpunit/php-timer": "^5.0.3", + "sebastian/cli-parser": "^1.0.2", + "sebastian/code-unit": "^1.0.8", "sebastian/comparator": "^4.0.8", - "sebastian/diff": "^4.0.3", - "sebastian/environment": "^5.1.3", - "sebastian/exporter": "^4.0.5", - "sebastian/global-state": "^5.0.1", - "sebastian/object-enumerator": "^4.0.3", - "sebastian/resource-operations": "^3.0.3", - "sebastian/type": "^3.2", + "sebastian/diff": "^4.0.6", + "sebastian/environment": "^5.1.5", + "sebastian/exporter": "^4.0.6", + "sebastian/global-state": "^5.0.7", + "sebastian/object-enumerator": "^4.0.4", + "sebastian/resource-operations": "^3.0.4", + "sebastian/type": "^3.2.1", "sebastian/version": "^3.0.2" }, "suggest": { @@ -1153,7 +1153,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.19" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.22" }, "funding": [ { @@ -1169,7 +1169,7 @@ "type": "tidelift" } ], - "time": "2024-04-05T04:35:58+00:00" + "time": "2024-12-05T13:48:26+00:00" }, { "name": "sebastian/cli-parser", @@ -2136,16 +2136,16 @@ }, { "name": "squizlabs/php_codesniffer", - "version": "3.10.1", + "version": "3.11.2", "source": { "type": "git", "url": "https://github.com/PHPCSStandards/PHP_CodeSniffer.git", - "reference": "8f90f7a53ce271935282967f53d0894f8f1ff877" + "reference": "1368f4a58c3c52114b86b1abe8f4098869cb0079" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/PHPCSStandards/PHP_CodeSniffer/zipball/8f90f7a53ce271935282967f53d0894f8f1ff877", - "reference": "8f90f7a53ce271935282967f53d0894f8f1ff877", + "url": "https://api.github.com/repos/PHPCSStandards/PHP_CodeSniffer/zipball/1368f4a58c3c52114b86b1abe8f4098869cb0079", + "reference": "1368f4a58c3c52114b86b1abe8f4098869cb0079", "shasum": "" }, "require": { @@ -2212,7 +2212,7 @@ "type": "open_collective" } ], - "time": "2024-05-22T21:24:41+00:00" + "time": "2024-12-11T16:04:26+00:00" }, { "name": "theseer/tokenizer", @@ -2364,6 +2364,6 @@ "ext-ctype": "*", "lib-pcre": "*" }, - "platform-dev": [], + "platform-dev": {}, "plugin-api-version": "2.6.0" } diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 0d3f2e3b919..e615183b222 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -27,6 +27,7 @@ Yii Framework 2 Change Log - Bug #20300: Clear stat cache in `FileCache::setValue()` (rob006) - Enh #20306: Add new `yii\helpers\ArrayHelper::flatten()` method (xcopy) - Bug #20308: Allow CompositeAuth auth methods to use their own user if defined (mtangoo) +- Bug #20239: Fix `yii\data\ActiveDataProvider` to avoid unexpected pagination results with UNION queries (Izumi-kun) 2.0.51 July 18, 2024 -------------------- diff --git a/framework/data/ActiveDataProvider.php b/framework/data/ActiveDataProvider.php index 3a129aa7293..c37c73f3151 100644 --- a/framework/data/ActiveDataProvider.php +++ b/framework/data/ActiveDataProvider.php @@ -11,6 +11,7 @@ use yii\base\Model; use yii\db\ActiveQueryInterface; use yii\db\Connection; +use yii\db\Query; use yii\db\QueryInterface; use yii\di\Instance; @@ -93,14 +94,40 @@ public function init() } /** - * {@inheritdoc} + * Creates a wrapper of [[query]] that allows adding limit and order. + * @return QueryInterface + * @throws InvalidConfigException */ - protected function prepareModels() + protected function createQueryWrapper(): QueryInterface { if (!$this->query instanceof QueryInterface) { throw new InvalidConfigException('The "query" property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses.'); } - $query = clone $this->query; + $wrapper = clone $this->query; + if ($wrapper instanceof Query && !empty($wrapper->union)) { + $wrapper->where = []; + $wrapper->limit = null; + $wrapper->offset = null; + $wrapper->orderBy = []; + $wrapper->selectOption = null; + $wrapper->distinct = false; + $wrapper->groupBy = []; + $wrapper->join = []; + $wrapper->having = []; + $wrapper->union = []; + $wrapper->params = []; + $wrapper->withQueries = []; + $wrapper->select('*')->from(['q' => $this->query]); + } + return $wrapper; + } + + /** + * {@inheritdoc} + */ + protected function prepareModels() + { + $query = $this->createQueryWrapper(); if (($pagination = $this->getPagination()) !== false) { $pagination->totalCount = $this->getTotalCount(); if ($pagination->totalCount === 0) { @@ -161,11 +188,7 @@ protected function prepareKeys($models) */ protected function prepareTotalCount() { - if (!$this->query instanceof QueryInterface) { - throw new InvalidConfigException('The "query" property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses.'); - } - $query = clone $this->query; - return (int) $query->limit(-1)->offset(-1)->orderBy([])->count('*', $this->db); + return (int) $this->createQueryWrapper()->limit(-1)->offset(-1)->orderBy([])->count('*', $this->db); } /** diff --git a/tests/framework/data/ActiveDataProviderTest.php b/tests/framework/data/ActiveDataProviderTest.php index af96dfef267..a6c4f1f1b95 100644 --- a/tests/framework/data/ActiveDataProviderTest.php +++ b/tests/framework/data/ActiveDataProviderTest.php @@ -35,6 +35,7 @@ protected function setUp(): void public function testActiveQuery() { $provider = new ActiveDataProvider([ + 'db' => $this->getConnection(), 'query' => Order::find()->orderBy('id'), ]); $orders = $provider->getModels(); @@ -45,6 +46,7 @@ public function testActiveQuery() $this->assertEquals([1, 2, 3], $provider->getKeys()); $provider = new ActiveDataProvider([ + 'db' => $this->getConnection(), 'query' => Order::find(), 'pagination' => [ 'pageSize' => 2, @@ -59,6 +61,7 @@ public function testActiveRelation() /* @var $customer Customer */ $customer = Customer::findOne(2); $provider = new ActiveDataProvider([ + 'db' => $this->getConnection(), 'query' => $customer->getOrders(), ]); $orders = $provider->getModels(); @@ -68,6 +71,7 @@ public function testActiveRelation() $this->assertEquals([2, 3], $provider->getKeys()); $provider = new ActiveDataProvider([ + 'db' => $this->getConnection(), 'query' => $customer->getOrders(), 'pagination' => [ 'pageSize' => 1, @@ -82,6 +86,7 @@ public function testActiveRelationVia() /* @var $order Order */ $order = Order::findOne(2); $provider = new ActiveDataProvider([ + 'db' => $this->getConnection(), 'query' => $order->getItems(), ]); $items = $provider->getModels(); @@ -92,6 +97,7 @@ public function testActiveRelationVia() $this->assertEquals([3, 4, 5], $provider->getKeys()); $provider = new ActiveDataProvider([ + 'db' => $this->getConnection(), 'query' => $order->getItems(), 'pagination' => [ 'pageSize' => 2, @@ -106,6 +112,7 @@ public function testActiveRelationViaTable() /* @var $order Order */ $order = Order::findOne(1); $provider = new ActiveDataProvider([ + 'db' => $this->getConnection(), 'query' => $order->getBooks(), ]); $items = $provider->getModels(); @@ -198,4 +205,20 @@ public function testDoesNotPerformQueryWhenHasNoModels() $this->assertEquals(0, $pagination->getPageCount()); } + + public function testPaginationWithUnionQuery() + { + $q1 = Item::find()->where(['category_id' => 2])->limit(2); + $q2 = Item::find()->where(['id' => 1]); + $provider = new ActiveDataProvider([ + 'db' => $this->getConnection(), + 'query' => $q1->union($q2), + ]); + $pagination = $provider->getPagination(); + $pagination->pageSize = 2; + $provider->prepare(); + $this->assertEquals(2, $pagination->getPageCount()); + $this->assertEquals(4, $provider->getTotalCount()); + $this->assertCount(2, $provider->getModels()); + } }