diff --git a/README.md b/README.md index 4fc4a46..cc6b245 100644 --- a/README.md +++ b/README.md @@ -247,7 +247,7 @@ $category = $db->category Name | Options ---- | ------- -`select` | `one()`, `all()`, `leftJoin($table, $on, $marks)`, `from($table)`, `field($field)`, `relatedWith($row)`, `marks($marks)`, `where($where, $marks)`, `orWhere($where, $marks)`, `by($field, $value)`, `byId($id)`, `limit($limit)`, `offset($offset)`, `orderBy($row, $direction)` +`select` | `one()`, `all()`, `leftJoin($table, $on, $marks)`, `from($table)`, `field($field)`, `relatedWith($row)`, `marks($marks)`, `where($where, $marks)`, `orWhere($where, $marks)`, `by($field, $value)`, `byId($id)`, `limit($limit)`, `offset($offset)`, `orderBy($row, $direction)`, `page($page, $limit)` `count` | `from($table)`, `field($field)`, `relatedWith($row)`, `marks($marks)`, `where($where, $marks)`, `orWhere($where, $marks)`, `by($field, $value)`, `byId($id)`, `limit($limit)`, `offset($offset)` `delete` | `marks($marks)`, `where($where, $marks)`, `orWhere($where, $marks)`, `by($field, $value)`, `byId($id)`, `limit($limit)`, `offset($offset)` `insert` | `data($data)`, `duplications($handle)` @@ -369,6 +369,29 @@ $post = $db->post[34]; //Returns the post 34 only if it's actived. You can define default modifiers for all queries: not only select, but also update, delete, etc. +### Pagination + +The `select` query has a special modifier to paginate the results: + +```php +$posts = $db->post->select() + ->page(1) + ->limit(50) + ->run(); + +//You can set the limit as second argument of page: +$posts = $db->post->select() + ->page(1, 50) + ->run(); + +//On paginate the results, you have three new methods in the result: +$posts->getPage(); //1 +$posts->getPrevPage(); //NULL +$posts->getNextPage(); //2 +``` + +**Note:** If the result length is lower than the max limit elements per page, it's assumed that there's no more pages, so `getNextPage()` returns `NULL`. + ### Fields The purpose of the `SimpleCrud\Fields` classes is to convert the data from/to the database for its usage. For example, in Mysql the format used to store datetime values is "Y-m-d H:i:s", so the class `SimpleCrud\Fields\Datetime` converts any string or `Datetime` instance to this format, and when you select this value, you get a Datetime instance. The available fields are: diff --git a/src/Queries/Mysql/Select.php b/src/Queries/Mysql/Select.php index cd5ceb8..332076c 100644 --- a/src/Queries/Mysql/Select.php +++ b/src/Queries/Mysql/Select.php @@ -23,6 +23,7 @@ class Select extends Query protected $orderBy = []; protected $statement; protected $mode = 2; + protected $page; /** * Change the mode to returns just the first row. @@ -48,6 +49,28 @@ public function all() return $this; } + /** + * Paginate the results + * + * @param int|string $page + * @param int|null $limit + * + * @return self + */ + public function page($page, $limit = null) { + $this->page = (int) $page; + + if ($this->page < 1) { + $this->page = 1; + } + + if ($limit !== null) { + $this->limit($limit); + } + + return $this; + } + /** * {@inheritdoc} * @@ -67,6 +90,24 @@ public function run() $result[] = $this->createRow($row); } + if ($this->page !== null) { + $current = $this->page; + $next = $result->count() < $this->limit ? null : $current + 1; + $prev = $current > 1 ? $current - 1 : null; + + $result->setMethod('getPage', function () use ($current) { + return $current; + }); + + $result->setMethod('getNextPage', function () use ($next) { + return $next; + }); + + $result->setMethod('getPrevPage', function () use ($prev) { + return $prev; + }); + } + return $result; } @@ -168,6 +209,10 @@ public function __invoke() */ public function __toString() { + if ($this->page !== null) { + $this->offset = ($this->page * $this->limit) - $this->limit; + } + $query = 'SELECT'; $query .= ' '.static::buildFields($this->table->name, array_keys($this->table->fields));