relations
[m6w6/pq-gateway] / lib / pq / Gateway / Row.php
index a8b879370f347b4ad494edd78d99a25d7b079328..249337334d68e48deee130f672d02786e54e20a3 100644 (file)
@@ -26,7 +26,7 @@ class Row implements \JsonSerializable
         */
        function __construct(Table $table, array $data = null, $prime = false) {
                $this->table = $table;
-               $this->data = $data;
+               $this->data = (array) $data;
                
                if ($prime) {
                        $this->prime();
@@ -43,13 +43,40 @@ class Row implements \JsonSerializable
                $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();
        }
        
        /**
@@ -124,13 +151,16 @@ class Row implements \JsonSerializable
        }
        
        /**
-        * 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];
        }
@@ -141,7 +171,38 @@ class Row implements \JsonSerializable
         * @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);
        }
        
        /**