Commit 246e2751 authored by Alexander Butenko's avatar Alexander Butenko

dbobject: fix for pagination function

parent 3cd0b3eb
......@@ -285,6 +285,16 @@ Object could be easily converted to a json string or an array.
$userArray = $user->toArray();
```
###Pagination
Use paginate() instead of get() to fetch paginated result
```php
$page = 1;
// set page limit to 2 results per page. 20 by default
product::$pageLimit = 2;
$products = product::arraybuilder()->paginate($page);
echo "showing $page out of " . product::$totalPages;
```
###Examples
Please look for a use examples in <a href='tests/dbObjectTests.php'>tests file</a> and test models inside the <a href='tests/models/'>test models</a> directory
......@@ -80,13 +80,13 @@ class dbObject {
*
* @var int
*/
public $pageLimit = 20;
public static $pageLimit = 20;
/**
* Variable that holds total pages count of last paginate() query
*
* @var int
*/
public $totalPages = 0;
public static $totalPages = 0;
/**
* An array that holds insert/update/select errors
*
......@@ -427,10 +427,10 @@ class dbObject {
* @return array
*/
private function paginate ($page, $fields = null) {
$offset = $this->pageLimit * ($page - 1);
$offset = self::$pageLimit * ($page - 1);
$this->db->withTotalCount();
$results = $this->get (Array ($this->pageLimit, $offset), $fields);
$this->totalPages = round ($this->db->totalCount / $this->pageLimit);
$results = $this->get (Array ($offset, self::$pageLimit), $fields);
self::$totalPages = round ($this->db->totalCount / self::$pageLimit);
return $results;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment