*/
function __construct(Table $table, array $data = null, $prime = false) {
$this->table = $table;
- $this->data = $data;
+ $this->data = (array) $data;
if ($prime) {
$this->prime();
$that->data = $data;
return $that->prime();
}
+
+ /**
+ * Export current state as an array
+ * @return array
+ * @throws \UnexpectedValueException if a cell has been modified by an expression
+ */
+ function export() {
+ $export = array_merge($this->data, $this->cell);
+ foreach ($export as &$val) {
+ if ($val instanceof Cell) {
+ if ($val->isExpr()) {
+ throw new \UnexpectedValueException("Cannot export an SQL expression");
+ }
+ $val = $val->get();
+ }
+ }
+ return $export;
+ }
+ /**
+ * Export current state with security sensitive data removed. You should override that, just
+ * calls export() by default.
+ * @return array
+ */
+ function exportPublic() {
+ return $this->export();
+ }
+
/**
* @implements JsonSerializable
* @return array
*/
function jsonSerialize() {
- return $this->data;
+ return $this->exportPublic();
}
/**
}
/**
- * Get a cell
+ * Get a cell or parent rows
* @param string $p
- * @return \pq\Gateway\Cell
+ * @return \pq\Gateway\Cell|\pq\Gateway\Rowset
*/
function __get($p) {
+ if ($this->table->hasRelation($p)) {
+ return $this->table->by($this, $p);
+ }
if (!isset($this->cell[$p])) {
- $this->cell[$p] = new Cell($this, $p, $this->data[$p]);
+ $this->cell[$p] = new Cell($this, $p, isset($this->data[$p]) ? $this->data[$p] : null);
}
return $this->cell[$p];
}
* @param mixed $v
*/
function __set($p, $v) {
- $this->__get($p)->set(($v instanceof Cell) ? $v->get() : $v);
+ $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);
}
/**