X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fpq-gateway;a=blobdiff_plain;f=lib%2Fpq%2FGateway%2FRowset.php;h=2b039935dfb42eb10d7cac0a9cc70009def49269;hp=2527b029a1f629cad222161c05e73cf84540a216;hb=20d2b6bcce8f1c7a1aaa375b86ffb5be30674956;hpb=0e684b0140154ebc8a304670c0349da840a16ad2 diff --git a/lib/pq/Gateway/Rowset.php b/lib/pq/Gateway/Rowset.php index 2527b02..2b03993 100644 --- a/lib/pq/Gateway/Rowset.php +++ b/lib/pq/Gateway/Rowset.php @@ -20,18 +20,16 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable protected $rows; /** - * @var string + * @var mixed */ - protected $row; + protected $row = "\\pq\\Gateway\\Row"; /** * @param \pq\Gateway\Table $table * @param \pq\Result $result */ - function __construct(Table $table, \pq\Result $result, $row = "\\pq\\Gateway\\Row") { + function __construct(Table $table, \pq\Result $result = null) { $this->table = $table; - $this->row = $row; - $this->hydrate($result); } @@ -51,20 +49,36 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable * @param \pq\Result $result * @return array */ - protected function hydrate(\pq\Result $result) { + protected function hydrate(\pq\Result $result = null) { $this->index = 0; $this->rows = array(); - $row = $this->row; - if (is_callable($row)) { - while (($data = $result->fetchRow(\pq\Result::FETCH_ASSOC))) { - $this->rows[] = $row($data); - } - } else { - while (($data = $result->fetchRow(\pq\Result::FETCH_ASSOC))) { - $this->rows[] = new $row($this->table, $data); + if ($result) { + $row = $this->row; + + if (is_callable($row)) { + while (($data = $result->fetchRow(\pq\Result::FETCH_ASSOC))) { + $this->rows[] = $row($data); + } + } elseif ($row) { + while (($data = $result->fetchRow(\pq\Result::FETCH_ASSOC))) { + $this->rows[] = new $row($this->table, $data); + } + } else { + $this->rows = $result->fetchAll(\pq\Result::FETCH_OBJECT); } } + + return $this; + } + + /** + * Set the row prototype + * @param mixed $row + * @return \pq\Gateway\Table + */ + function setRowPrototype($row) { + $this->row = $row; return $this; }