Skip to content

Commit

Permalink
Fix Pagination::totalCount initialized incorrectly yiisoft#16891
Browse files Browse the repository at this point in the history
  • Loading branch information
taobig committed Dec 8, 2018
1 parent 2fab707 commit 36cf676
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
18 changes: 15 additions & 3 deletions framework/data/BaseDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -165,7 +168,14 @@ public function getTotalCount()
{
if ($this->getPagination() === false) {
return $this->getCount();
} elseif ($this->_totalCount === null) {
}

return $this->getTotalCountWithPagination();
}

private function getTotalCountWithPagination()
{
if ($this->_totalCount === null) {
$this->_totalCount = $this->prepareTotalCount();
}

Expand All @@ -183,8 +193,6 @@ public function setTotalCount($value)

/**
* 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()
Expand All @@ -193,6 +201,10 @@ public function getPagination()
$this->setPagination([]);
}

if (($this->_pagination !== false) && ($this->_pagination->totalCount == -1)) {
$this->_pagination->totalCount = $this->getTotalCountWithPagination();
}

return $this->_pagination;
}

Expand Down
2 changes: 1 addition & 1 deletion framework/data/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class Pagination extends BaseObject implements Linkable
/**
* @var int total number of items.
*/
public $totalCount = 0;
public $totalCount = -1;
/**
* @var int the default page size. This property will be returned by [[pageSize]] when page size
* cannot be determined by [[pageSizeParam]] from [[params]].
Expand Down
41 changes: 41 additions & 0 deletions tests/framework/data/ActiveDataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,45 @@ public function testDoesNotPerformQueryWhenHasNoModels()

$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);

}

}

0 comments on commit 36cf676

Please sign in to comment.