Skip to content

Commit

Permalink
added pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Oct 29, 2016
1 parent 0ce6cd2 commit 7988438
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
45 changes: 45 additions & 0 deletions src/Queries/Mysql/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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}
*
Expand All @@ -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;
}

Expand Down Expand Up @@ -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));

Expand Down

0 comments on commit 7988438

Please sign in to comment.