From d9b256d73455ca771ca985efa7b23d7bfa8b7312 Mon Sep 17 00:00:00 2001 From: Jin Hu Date: Thu, 26 Sep 2013 20:29:59 +0800 Subject: [PATCH] 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())); + } }