From aa72c19a8ff70e33481a8bad0f82876bea97b8c5 Mon Sep 17 00:00:00 2001 From: mark_story Date: Fri, 19 Sep 2014 20:33:29 -0400 Subject: [PATCH] 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 --- src/Database/Query.php | 7 ++++++- tests/TestCase/Database/QueryTest.php | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Database/Query.php b/src/Database/Query.php index 0e0634deacd..7b95b7b23f0 100644 --- a/src/Database/Query.php +++ b/src/Database/Query.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; diff --git a/tests/TestCase/Database/QueryTest.php b/tests/TestCase/Database/QueryTest.php index e4d0dc18bd5..b6480aa2b24 100644 --- a/tests/TestCase/Database/QueryTest.php +++ b/tests/TestCase/Database/QueryTest.php @@ -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'));