mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-17 23:09:10 +08:00
Merge pull request #909 from bixuehujin/dataprovider
Fixed pagination not working before data loaded
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user