From d9b256d73455ca771ca985efa7b23d7bfa8b7312 Mon Sep 17 00:00:00 2001 From: Jin Hu Date: Thu, 26 Sep 2013 20:29:59 +0800 Subject: [PATCH 1/2] Fixed pagination not working before data loaded --- framework/yii/data/DataProvider.php | 1 + tests/unit/framework/data/ActiveDataProviderTest.php | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/framework/yii/data/DataProvider.php b/framework/yii/data/DataProvider.php index b29f616..d75be6f 100644 --- a/framework/yii/data/DataProvider.php +++ b/framework/yii/data/DataProvider.php @@ -48,6 +48,7 @@ abstract class DataProvider extends Component implements DataProviderInterface if ($this->id !== null) { $this->_pagination->pageVar = $this->id . '-page'; } + $this->_pagination->totalCount = $this->getTotalCount(); } return $this->_pagination; } diff --git a/tests/unit/framework/data/ActiveDataProviderTest.php b/tests/unit/framework/data/ActiveDataProviderTest.php index 3f65ebb..79c0a39 100644 --- a/tests/unit/framework/data/ActiveDataProviderTest.php +++ b/tests/unit/framework/data/ActiveDataProviderTest.php @@ -85,4 +85,21 @@ class ActiveDataProviderTest extends DatabaseTestCase $provider->refresh(); $this->assertEquals(2, count($provider->getModels())); } + + public function testPaginationBeforeModels() + { + $query = new Query; + $provider = new ActiveDataProvider(array( + 'db' => $this->getConnection(), + 'query' => $query->from('tbl_order')->orderBy('id'), + )); + $pagination = $provider->getPagination(); + $this->assertEquals(1, $pagination->getPageCount()); + $this->assertCount(3, $provider->getModels()); + + $provider->getPagination()->pageSize = 2; + $this->assertEquals(3, count($provider->getModels())); + $provider->refresh(); + $this->assertEquals(2, count($provider->getModels())); + } } From b7948e48406f21a059431a619c901b3cd88a8ec5 Mon Sep 17 00:00:00 2001 From: Jin Hu Date: Thu, 26 Sep 2013 23:35:02 +0800 Subject: [PATCH 2/2] Removed unnecessary lines --- framework/yii/data/ActiveDataProvider.php | 1 - framework/yii/data/ArrayDataProvider.php | 1 - 2 files changed, 2 deletions(-) diff --git a/framework/yii/data/ActiveDataProvider.php b/framework/yii/data/ActiveDataProvider.php index aaf71b2..2fe0efb 100644 --- a/framework/yii/data/ActiveDataProvider.php +++ b/framework/yii/data/ActiveDataProvider.php @@ -156,7 +156,6 @@ class ActiveDataProvider extends DataProvider throw new InvalidConfigException('The "query" property must be an instance of Query or its subclass.'); } if (($pagination = $this->getPagination()) !== false) { - $pagination->totalCount = $this->getTotalCount(); $this->query->limit($pagination->getLimit())->offset($pagination->getOffset()); } if (($sort = $this->getSort()) !== false) { diff --git a/framework/yii/data/ArrayDataProvider.php b/framework/yii/data/ArrayDataProvider.php index d6eaca7..9534803 100644 --- a/framework/yii/data/ArrayDataProvider.php +++ b/framework/yii/data/ArrayDataProvider.php @@ -114,7 +114,6 @@ class ArrayDataProvider extends DataProvider } if (($pagination = $this->getPagination()) !== false) { - $pagination->totalCount = $this->getTotalCount(); $models = array_slice($models, $pagination->getOffset(), $pagination->getLimit()); }