Skip to content
Permalink
Browse files

Add limit parameter to page()

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 aa72c19a8ff70e33481a8bad0f82876bea97b8c5
Showing with 12 additions and 1 deletion.
  1. +6 −1 src/Database/Query.php
  2. +6 −0 tests/TestCase/Database/QueryTest.php
@@ -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;
@@ -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'));

0 comments on commit aa72c19

Please sign in to comment.
You can’t perform that action at this time.