refactor relations
[m6w6/pq-gateway] / lib / pq / Gateway / Row.php
index 78ec4c4e9c2d38e6ef82c118a07c3bf19eb6772f..bfda71b0575cfe73aa43d3f08051a2bed79c11d5 100644 (file)
@@ -197,14 +197,11 @@ class Row implements \JsonSerializable
        }
        
        /**
-        * Get a cell or parent rows
+        * Get a cell
         * @param string $p
-        * @return \pq\Gateway\Cell|\pq\Gateway\Rowset
+        * @return \pq\Gateway\Cell
         */
        function __get($p) {
-               if ($this->table->hasRelation($p)) {
-                       return $this->table->by($this, $p);
-               }
                return $this->cell($p);
        }
        
@@ -235,17 +232,40 @@ class Row implements \JsonSerializable
                return isset($this->data[$p]) || isset($this->cell[$p]);
        }
        
+       /**
+        * Get the parent row
+        * @see \pq\Gateway\Table::by()
+        * @param mixed $foreign table (name)
+        * @param string $ref
+        * @return \pq\Gateway\Rowset
+        */
+       function ofWhich($foreign, $ref = null) {
+               if (!($foreign instanceof Table)) {
+                       $foreign = forward_static_call(
+                               [get_class($this->getTable()), "resolve"], 
+                               $foreign
+                       );
+               }
+               return $foreign->by($this, $ref);
+       }
+       
        /**
         * Get child rows of this row by foreign key
         * @see \pq\Gateway\Table::of()
-        * @param string $foreign
-        * @param array $args [order, limit, offset]
+        * @param mixed $foreign table (name)
+        * @param string $order
+        * @param int $limit
+        * @param int $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);
+       function allOf($foreign, $ref = null, $order = null, $limit = 0, $offset = 0) {
+               if (!($foreign instanceof Table)) {
+                       $foreign = forward_static_call(
+                               [get_class($this->getTable()), "resolve"], 
+                               $foreign
+                       );
+               }
+               return $foreign->of($this, $ref, $order, $limit, $offset);
        }
        
        /**