diff --git a/UPDATE.md b/UPDATE.md index 81493360a4..aa0ee71a19 100644 --- a/UPDATE.md +++ b/UPDATE.md @@ -118,7 +118,7 @@ After: ## `Propel\Generator\Model\ForeignKey::getForeignColumnNames` method has been removed. -## `Propel\Generator\Model\Domain::printSize` method has been renamed to +## `Propel\Generator\Model\Domain::printSize` method has been renamed to `Propel\Generator\Model\Domain::getSizeDefinition`. ## `Propel\Generator\Model\Table::printPrimaryKey` method has been removed. @@ -152,3 +152,6 @@ This could break, behaviors that call `parent` methods All methods from `Propel\Runtime\Om\Persistent` Interface have been removed All Base Object classes now implements `Propel\Runtime\Om\ActiveRecordInterface` this could be use to identify a Propel Object + +## `Propel\Runtime\Util\PropelModelPager::count()` now return the number of items in the collection. +To get the old behavior use `Propel\Runtime\Util\PropelModelPager::getNbResults()`. diff --git a/src/Propel/Runtime/Util/PropelModelPager.php b/src/Propel/Runtime/Util/PropelModelPager.php index c0a1b907f1..aec9fb0969 100644 --- a/src/Propel/Runtime/Util/PropelModelPager.php +++ b/src/Propel/Runtime/Util/PropelModelPager.php @@ -11,6 +11,8 @@ namespace Propel\Runtime\Util; use Propel\Runtime\ActiveQuery\ModelCriteria; +use Propel\Runtime\Collection\Collection; +use Propel\Runtime\Connection\ConnectionInterface; /** * Implements a pager based on a ModelCriteria @@ -18,36 +20,56 @@ * * @author Fabien Potencier * @author François Zaninotto - * @version $Revision$ */ class PropelModelPager implements \IteratorAggregate, \Countable { + /** + * @var ModelCriteria + */ protected $query; + /** + * @var int current page + */ protected $page; + /** + * @var int number of item per page + */ protected $maxPerPage; + /** + * @var int index of the last page + */ protected $lastPage; + /** + * @var int number of item the query return without pagination + */ protected $nbResults; - protected $objects; - - protected $parameters; - + /** + * @var int + */ protected $currentMaxLink; + /** + * @var int + */ protected $maxRecordLimit; + /** + * @var Collection|array|mixed + */ protected $results; + /** + * @var ConnectionInterface + */ protected $con; public function __construct(ModelCriteria $query, $maxPerPage = 10) { - $this->parameters = array(); - $this->setQuery($query); $this->setMaxPerPage($maxPerPage); $this->setPage(1); @@ -68,7 +90,7 @@ public function getQuery() return $this->query; } - public function init($con = null) + public function init(ConnectionInterface $con = null) { $this->con = $con; $hasMaxRecordLimit = false !== $this->getMaxRecordLimit(); @@ -140,16 +162,16 @@ public function setMaxRecordLimit($limit) $this->maxRecordLimit = $limit; } - public function getLinks($nb_links = 5) + public function getLinks($nbLinks = 5) { $links = array(); - $tmp = $this->page - floor($nb_links / 2); - $check = $this->lastPage - $nb_links + 1; + $tmp = $this->page - floor($nbLinks / 2); + $check = $this->lastPage - $nbLinks + 1; $limit = ($check > 0) ? $check : 1; $begin = ($tmp > 0) ? (($tmp > $limit) ? $limit : $tmp) : 1; $i = (int) $begin; - while (($i < $begin + $nb_links) && ($i <= $this->lastPage)) { + while (($i < $begin + $nbLinks) && ($i <= $this->lastPage)) { $links[] = $i++; } @@ -414,14 +436,14 @@ public function getIterator() } /** - * Returns the total number of results. + * Returns the number of items in the result collection. * * @see Countable * @return int */ public function count() { - return $this->getNbResults(); + return count($this->getResults()); } } diff --git a/tests/Propel/Tests/Runtime/Util/PropelModelPagerTest.php b/tests/Propel/Tests/Runtime/Util/PropelModelPagerTest.php index dd3973c681..7e45c8b192 100644 --- a/tests/Propel/Tests/Runtime/Util/PropelModelPagerTest.php +++ b/tests/Propel/Tests/Runtime/Util/PropelModelPagerTest.php @@ -256,4 +256,18 @@ public function testIsOddAndIsEven() } } + public function testCountableInterface() + { + BookQuery::create()->deleteAll(); + $pager = $this->getPager(10); + $this->assertCount(0, $pager); + + $this->createBooks(15); + $pager = $this->getPager(10); + $this->assertCount(10, $pager); + + $pager = $this->getPager(10, 2); + $this->assertCount(5, $pager); + } + }