Browse Source

Merge pull request #909 from bixuehujin/dataprovider

Fixed pagination not working before data loaded
tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
647aa71a5a
  1. 1
      framework/yii/data/ActiveDataProvider.php
  2. 1
      framework/yii/data/ArrayDataProvider.php
  3. 1
      framework/yii/data/DataProvider.php
  4. 17
      tests/unit/framework/data/ActiveDataProviderTest.php

1
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.'); throw new InvalidConfigException('The "query" property must be an instance of Query or its subclass.');
} }
if (($pagination = $this->getPagination()) !== false) { if (($pagination = $this->getPagination()) !== false) {
$pagination->totalCount = $this->getTotalCount();
$this->query->limit($pagination->getLimit())->offset($pagination->getOffset()); $this->query->limit($pagination->getLimit())->offset($pagination->getOffset());
} }
if (($sort = $this->getSort()) !== false) { if (($sort = $this->getSort()) !== false) {

1
framework/yii/data/ArrayDataProvider.php

@ -114,7 +114,6 @@ class ArrayDataProvider extends DataProvider
} }
if (($pagination = $this->getPagination()) !== false) { if (($pagination = $this->getPagination()) !== false) {
$pagination->totalCount = $this->getTotalCount();
$models = array_slice($models, $pagination->getOffset(), $pagination->getLimit()); $models = array_slice($models, $pagination->getOffset(), $pagination->getLimit());
} }

1
framework/yii/data/DataProvider.php

@ -48,6 +48,7 @@ abstract class DataProvider extends Component implements DataProviderInterface
if ($this->id !== null) { if ($this->id !== null) {
$this->_pagination->pageVar = $this->id . '-page'; $this->_pagination->pageVar = $this->id . '-page';
} }
$this->_pagination->totalCount = $this->getTotalCount();
} }
return $this->_pagination; return $this->_pagination;
} }

17
tests/unit/framework/data/ActiveDataProviderTest.php

@ -85,4 +85,21 @@ class ActiveDataProviderTest extends DatabaseTestCase
$provider->refresh(); $provider->refresh();
$this->assertEquals(2, count($provider->getModels())); $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()));
}
} }

Loading…
Cancel
Save