* @param \pq\Result $result
* @return \pq\Gateway\Rowset
*/
- function __invoke(\pq\Result $result) {
+ function __invoke(\pq\Result $result = null) {
$that = clone $this;
$that->hydrate($result);
return $that;
$this->rows = array();
if ($result) {
- $row = $this->row;
+ $row = $this->getRowPrototype();
if (is_callable($row)) {
while (($data = $result->fetchRow(\pq\Result::FETCH_ASSOC))) {
return $this;
}
+ /**
+ * Get the row prototype
+ * @return mixed
+ */
+ function getRowPrototype() {
+ return $this->row;
+ }
+
/**
* @return \pq\Gateway\Table
*/
/**
* Create all rows of this rowset
- * @param bool $txn
+ * @param mixed $txn
* @return \pq\Gateway\Rowset
* @throws Exception
*/
function create($txn = true) {
- $txn = $txn ? $this->table->getConnection()->startTransaction() : false;
+ if ($txn && !($txn instanceof pq\Transaction)) {
+ $txn = $this->table->getConnection()->startTransaction();
+ }
try {
foreach ($this->rows as $row) {
$row->create();
/**
* Update all rows of this rowset
- * @param bool $txn
+ * @param mixed $txn
* @return \pq\Gateway\Rowset
* @throws \Exception
*/
function update($txn = true) {
- $txn = $txn ? $this->table->getConnection()->startTransaction() : false;
+ if ($txn && !($txn instanceof pq\Transaction)) {
+ $txn = $this->table->getConnection()->startTransaction();
+ }
try {
foreach ($this->rows as $row) {
$row->update();
/**
* Delete all rows of this rowset
- * @param type $txn
+ * @param mixed $txn
* @return \pq\Gateway\Rowset
* @throws \Exception
*/
function delete($txn = true) {
- $txn = $txn ? $this->table->getConnection()->startTransaction() : false;
+ if ($txn && !($txn instanceof pq\Transaction)) {
+ $txn = $this->table->getConnection()->startTransaction();
+ }
try {
foreach ($this->rows as $row) {
$row->delete();
function rewind() {
$this->index = 0;
}
+
/**
* @implements \Iterator
*/
function next() {
++$this->index;
}
+
/**
* @implements \Iterator
* @return bool
function valid() {
return $this->index < count($this->rows);
}
+
/**
* @implements \Iterator
* @return \pq\Gateway\Row
*/
function current() {
+ if (!$this->valid()) {
+ throw new \OutOfBoundsException("Invalid row index {$this->index}");
+ }
return $this->rows[$this->index];
}
+
/**
* @implements \Iterator
* @return int
*/
function filter(callable $cb) {
$rowset = clone $this;
+ $rowset->index = 0;
$rowset->rows = array_filter($this->rows, $cb);
return $rowset;
}