Skip to content

Commit

Permalink
Add limit parameter to page()
Browse files Browse the repository at this point in the history
Being able to set the limit and offset with the page() method
makes it a bit easier to use as the operation is less order dependent.

Refs #4641
  • Loading branch information
markstory committed Sep 20, 2014
1 parent c24f7ca commit aa72c19
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 aa72c19

Please sign in to comment.