Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Nov 6, 2016
2 parents b4ae713 + 663ec84 commit a39ee3f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Expand Down 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 a39ee3f

Please sign in to comment.