Skip to content

Commit

Permalink
Merge pull request zofe#425 from sroutier/PDOStatementSource
Browse files Browse the repository at this point in the history
Added ability to specify a PDOStatement as a data source.
  • Loading branch information
zofe authored Apr 23, 2018
2 parents 15283a2 + ce0cbf3 commit 50a71e1
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/DataSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ public function build()
$this->key = $this->query->getModel()->getKeyName();
}

} elseif ( is_a($this->source, "\PDOStatement")) {
$this->type = "PDOStatement";

}
//array
elseif (is_array($this->source)) {
Expand All @@ -176,6 +179,38 @@ public function build()

//build subset of data
switch ($this->type) {
case "PDOStatement":
//orderby is handled by the code providing the PDOStatement

//calculate page variable.
$limit = $this->limit ? $this->limit : 100000;
$current_page = $this->url->value('page'.$this->cid, 0);
$offset = (max($current_page-1,0)) * $limit;

$rows = array();
$skip = $cnt = 0;
foreach ($this->source as $row) {
//skip ahead past the offset.
if ($skip < $offset) {
$skip++;
continue;
}
//gather the rows to render
elseif ($cnt <= $limit ) {
$rows[$cnt] = $row;
$cnt++;
}
}

$this->data = $rows;
// PDOStatement often do not know how many rows there are until all have been fetched.
$this->total_rows = null;
$this->paginator = new Paginator($this->data, $limit, $current_page,
['path' => Paginator::resolveCurrentPath(),
'pageName' => "page".$this->cid,
]);
break;

case "array":
//orderby
if (isset($this->orderby)) {
Expand Down

0 comments on commit 50a71e1

Please sign in to comment.