X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fpq-gateway;a=blobdiff_plain;f=lib%2Fpq%2FGateway%2FRow.php;h=bba7c09c07642c90fe1d877f18c7462da40e7bfe;hp=5b438e564d80ca994088bca2d1585477b7c5a9d2;hb=580991717f5e8bb237403757e2111a8d04aca616;hpb=5c1b63644ccb9277f4dec5e8a14ab6592f1677c5 diff --git a/lib/pq/Gateway/Row.php b/lib/pq/Gateway/Row.php index 5b438e5..bba7c09 100644 --- a/lib/pq/Gateway/Row.php +++ b/lib/pq/Gateway/Row.php @@ -2,6 +2,8 @@ namespace pq\Gateway; +use \pq\Query\Expr as QueryExpr; + class Row implements \JsonSerializable { /** @@ -93,6 +95,30 @@ class Row implements \JsonSerializable return $this->data; } + /** + * Get all column/value pairs to possibly uniquely identify this row + * @return array + * @throws \OutOfBoundsException if any primary key column is not present in the row + */ + function getIdentity() { + $cols = array(); + if (count($identity = $this->getTable()->getIdentity())) { + foreach ($identity as $col) { + if (!array_key_exists($col, $this->data)) { + throw new \OutOfBoundsException( + sprintf("Column '%s' does not exist in row of table '%s'", + $col, $this->getTable()->getName() + ) + ); + } + $cols[$col] = $this->data[$col]; + } + } else { + $cols = $this->data; + } + return $cols; + } + /** * Check whether the row contains modifications * @return boolean @@ -106,6 +132,10 @@ class Row implements \JsonSerializable return false; } + /** + * Refresh the rows data + * @return \pq\Gateway\Row + */ function refresh() { $this->data = $this->table->find($this->criteria(), null, 1, 0)->current()->data; $this->cell = array(); @@ -125,13 +155,21 @@ class Row implements \JsonSerializable } /** - * Transform data array to where criteria + * Transform the row's identity to where criteria * @return array */ protected function criteria() { $where = array(); - foreach($this->data as $k => $v) { - $where["$k="] = $v; + foreach ($this->getIdentity() as $col => $val) { + if (isset($val)) { + $where["$col="] = $val; + } else { + $where["$col IS"] = new QueryExpr("NULL"); + } + } + + if (($lock = $this->getTable()->getLock())) { + $lock->criteria($this, $where); } return $where; } @@ -151,14 +189,13 @@ class Row implements \JsonSerializable } /** - * Get a cell + * Get a cell or parent rows * @param string $p * @return \pq\Gateway\Cell|\pq\Gateway\Rowset */ function __get($p) { - if (isset($this->data["{$p}_id"])) { - // FIXME cache - return $this->getTable()->by($this, $p); + if ($this->table->hasRelation($p)) { + return $this->table->by($this, $p); } if (!isset($this->cell[$p])) { $this->cell[$p] = new Cell($this, $p, isset($this->data[$p]) ? $this->data[$p] : null); @@ -175,21 +212,47 @@ class Row implements \JsonSerializable $this->__get($p)->set($v); } + /** + * Unset a cell value + * @param string $p + */ function __unset($p) { unset($this->data[$p]); unset($this->cell[$p]); } + /** + * Check if a cell isset + * @param string $p + * @return bool + */ function __isset($p) { return isset($this->data[$p]) || isset($this->cell[$p]); } + /** + * Get child rows of this row by foreign key + * @see \pq\Gateway\Table::of() + * @param string $foreign + * @param array $args [order, limit, offset] + * @return \pq\Gateway\Rowset + */ + function __call($foreign, array $args) { + array_unshift($args, $this); + $table = forward_static_call(array(get_class($this->getTable()), "resolve"), $foreign); + return call_user_func_array(array($table, "of"), $args); + } + /** * Create this row in the database * @return \pq\Gateway\Row */ function create() { - $this->data = $this->table->create($this->changes())->current()->data; + $rowset = $this->table->create($this->changes()); + if (!count($rowset)) { + throw new \UnexpectedValueException("No row created"); + } + $this->data = $rowset->current()->data; $this->cell = array(); return $this; } @@ -199,7 +262,11 @@ class Row implements \JsonSerializable * @return \pq\Gateway\Row */ function update() { - $this->data = $this->table->update($this->criteria(), $this->changes())->current()->data; + $rowset = $this->table->update($this->criteria(), $this->changes()); + if (!count($rowset)) { + throw new \UnexpectedValueException("No row updated"); + } + $this->data = $rowset->current()->data; $this->cell = array(); return $this; } @@ -209,7 +276,11 @@ class Row implements \JsonSerializable * @return \pq\Gateway\Row */ function delete() { - $this->data = $this->table->delete($this->criteria(), "*")->current()->data; + $rowset = $this->table->delete($this->criteria(), "*"); + if (!count($rowset)) { + throw new \UnexpectedValueException("No row deleted"); + } + $this->data = $rowset->current()->data; return $this->prime(); } }