From e1623868f9e95769074312f8582c1709cfcb52a8 Mon Sep 17 00:00:00 2001 From: taobig Date: Wed, 2 Jan 2019 02:24:14 +0800 Subject: [PATCH] Fixes #16891: Fixed Pagination::totalCount initialized incorrectly --- framework/CHANGELOG.md | 1 + framework/data/BaseDataProvider.php | 12 +++++- framework/data/Pagination.php | 4 +- .../framework/data/ActiveDataProviderTest.php | 43 ++++++++++++++++++- 4 files changed, 55 insertions(+), 5 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 0da042b6f0..a01d177fa3 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,6 +4,7 @@ Yii Framework 2 Change Log 2.0.16 under development ------------------------ +- Bug #16891: Fixed Pagination::totalCount initialized incorrectly (taobig) - Bug #16028: Fix serialization of complex cache keys that contain non-UTF sequences (rugabarbo) - Bug #16945: Fixed RBAC DbManager ruleName fetching on the case of PDO::ATTR_ORACLE_NULLS => PDO::NULL_TO_STRING (razonyang) - Bug #16081: Fixed composite IN using just one column (rugabarbo) diff --git a/framework/data/BaseDataProvider.php b/framework/data/BaseDataProvider.php index 82ac0d7fb9..1b4f090b26 100644 --- a/framework/data/BaseDataProvider.php +++ b/framework/data/BaseDataProvider.php @@ -46,6 +46,9 @@ abstract class BaseDataProvider extends Component implements DataProviderInterfa public $id; private $_sort; + /** + * @var Pagination|false $_pagination + */ private $_pagination; private $_keys; private $_models; @@ -183,8 +186,6 @@ abstract class BaseDataProvider extends Component implements DataProviderInterfa /** * Returns the pagination object used by this data provider. - * Note that you should call [[prepare()]] or [[getModels()]] first to get correct values - * of [[Pagination::totalCount]] and [[Pagination::pageCount]]. * @return Pagination|false the pagination object. If this is false, it means the pagination is disabled. */ public function getPagination() @@ -193,6 +194,13 @@ abstract class BaseDataProvider extends Component implements DataProviderInterfa $this->setPagination([]); } + if (($this->_pagination !== false) && ($this->_pagination->totalCount === null)) { + if ($this->_totalCount === null) { + $this->setTotalCount($this->prepareTotalCount()); + } + $this->_pagination->totalCount = $this->_totalCount; + } + return $this->_pagination; } diff --git a/framework/data/Pagination.php b/framework/data/Pagination.php index c8c12d0349..5e78729e24 100644 --- a/framework/data/Pagination.php +++ b/framework/data/Pagination.php @@ -124,9 +124,9 @@ class Pagination extends BaseObject implements Linkable */ public $validatePage = true; /** - * @var int total number of items. + * @var int|null total number of items. */ - public $totalCount = 0; + public $totalCount; /** * @var int the default page size. This property will be returned by [[pageSize]] when page size * cannot be determined by [[pageSizeParam]] from [[params]]. diff --git a/tests/framework/data/ActiveDataProviderTest.php b/tests/framework/data/ActiveDataProviderTest.php index b496dcb96b..6d120edd37 100644 --- a/tests/framework/data/ActiveDataProviderTest.php +++ b/tests/framework/data/ActiveDataProviderTest.php @@ -170,7 +170,7 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase 'query' => $query->from('order')->orderBy('id'), ]); $pagination = $provider->getPagination(); - $this->assertEquals(0, $pagination->getPageCount()); + $this->assertEquals(1, $pagination->getPageCount()); $this->assertCount(3, $provider->getModels()); $this->assertEquals(1, $pagination->getPageCount()); @@ -198,4 +198,45 @@ abstract class ActiveDataProviderTest extends DatabaseTestCase $this->assertEquals(0, $pagination->getPageCount()); } + + public function testValidateTotalCount() + { + $provider = new ActiveDataProvider([ + 'query' => Order::find()->orderBy('id'), + 'totalCount' => 100, + 'pagination' => [ + ], + ]); + $this->assertSame(100, $provider->pagination->totalCount); + + $provider = new ActiveDataProvider([ + 'query' => Order::find()->orderBy('id'), + 'totalCount' => 100, + 'pagination' => false, + ]); + $this->assertSame(false, $provider->pagination); + } + + public function testValidatePageCount() + { + $provider = new ActiveDataProvider([ + 'query' => Order::find()->orderBy('id'), + 'totalCount' => 100, + 'pagination' => [ + 'pageSize' => 101, + ], + ]); + $this->assertSame(1, $provider->pagination->pageCount); + + $provider = new ActiveDataProvider([ + 'query' => Order::find()->orderBy('id'), + 'totalCount' => 100, + 'pagination' => [ + 'pageSize' => 1, + ], + ]); + $this->assertSame(100, $provider->pagination->pageCount); + + } + }