Skip to content

Commit

Permalink
Merge pull request #4669 from cakephp/issue-4641
Browse files Browse the repository at this point in the history
Add limit parameter to page()
  • Loading branch information
lorenzo committed Sep 20, 2014
2 parents a3b9e79 + aa72c19 commit 8e3bada
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Database/Query.php
Expand Up @@ -1016,9 +1016,14 @@ public function orHaving($conditions, $types = []) {
* Pages should start at 1.
*
* @param int $num The page number you want.
* @param int $limit The number of rows you want in the page. If null
* the current limit clause will be used.
* @return $this
*/
public function page($num) {
public function page($num, $limit = null) {
if ($limit !== null) {
$this->limit($limit);
}
$limit = $this->clause('limit');
if ($limit === null) {
$limit = 25;
Expand Down
6 changes: 6 additions & 0 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -1621,10 +1621,16 @@ public function testSelectPage() {
$result = $query->select('id')->from('comments')
->limit(1)
->page(2)
->order(['id' => 'asc'])
->execute();
$this->assertCount(1, $result);
$this->assertEquals(['id' => 2], $result->fetch('assoc'));

$query = new Query($this->connection);
$query->select('id')->from('comments')->page(3, 10);
$this->assertEquals(10, $query->clause('limit'));
$this->assertEquals(20, $query->clause('offset'));

$query = new Query($this->connection);
$query->select('id')->from('comments')->page(1);
$this->assertEquals(25, $query->clause('limit'));
Expand Down

0 comments on commit 8e3bada

Please sign in to comment.