From 218783ecee7d84b22f76ef73c82120b534df06e0 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 21 Jul 2011 07:17:53 -0700 Subject: [PATCH] Refactoring Query class pagination. --- system/db/query.php | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/system/db/query.php b/system/db/query.php index f0c160b2..c6ed2272 100644 --- a/system/db/query.php +++ b/system/db/query.php @@ -430,7 +430,14 @@ public function get($columns = array('*')) $this->select($columns); } - return DB::query(Query\Compiler::select($this), $this->bindings, $this->connection); + $results = DB::query(Query\Compiler::select($this), $this->bindings, $this->connection); + + // Reset the SELECT clause so more queries can be performed using the same instance. + // This is helpful for performing counts and then getting actual results, such as + // when paginating results. + $this->select = null; + + return $results; } /** @@ -457,13 +464,21 @@ public function paginate($per_page) { $total = $this->count(); - // Reset the SELECT clause so we can execute another query to get the results. - $this->select = null; - - // Get the current page. The Paginator class will validate the page number. $page = \System\Paginator::page(ceil($total / $per_page)); - return new \System\Paginator($this->skip(($page - 1) * $per_page)->take($per_page)->get(), $total, $per_page); + return new \System\Paginator($this->for_page($page, $per_page)->get(), $total, $per_page); + } + + /** + * Set the LIMIT and OFFSET values for a given page. + * + * @param int $page + * @param int $per_page + * @return Query + */ + public function for_page($page, $per_page) + { + return $this->skip(($page - 1) * $per_page)->take($per_page); } /**