From ce0cbf3c5230b1ffa737fcd6b2716d1201fcd08d Mon Sep 17 00:00:00 2001 From: Sebastien Routier Date: Sun, 22 Apr 2018 18:24:13 -0400 Subject: [PATCH] Added ability to specify a PDOStatement as a data source. --- src/DataSet.php | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/DataSet.php b/src/DataSet.php index f8bcc2d8..9b3d7eb1 100644 --- a/src/DataSet.php +++ b/src/DataSet.php @@ -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)) { @@ -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)) {