forked from propelorm/Propel2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(PropelModelPager): change PropelModelPager::count() propelorm#208
PropelModelPager::count() now return the number of items in the collection remove unused variables, add phpdocs
- Loading branch information
Showing
3 changed files
with
54 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,43 +11,65 @@ | |
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 | ||
* The code from this class heavily borrows from symfony's sfPager class | ||
* | ||
* @author Fabien Potencier <[email protected]> | ||
* @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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters