trigger mirror
[m6w6/pq-gateway] / lib / pq / Gateway / Row.php
index 6d9c19e750b1b215da48b1b90959692f3f6c9fd8..bfda71b0575cfe73aa43d3f08051a2bed79c11d5 100644 (file)
@@ -185,27 +185,33 @@ class Row implements \JsonSerializable
        }
        
        /**
-        * Get a cell or parent rows
-        * @param string $p
-        * @return \pq\Gateway\Cell|\pq\Gateway\Rowset
+        * Cell accessor
+        * @param string $p column name
+        * @return \pq\Gateway\Cell
         */
-       function __get($p) {
-               if ($this->table->hasRelation($p)) {
-                       return $this->table->by($this, $p);
-               }
+       protected function cell($p) {
                if (!isset($this->cell[$p])) {
                        $this->cell[$p] = new Cell($this, $p, isset($this->data[$p]) ? $this->data[$p] : null);
                }
                return $this->cell[$p];
        }
        
+       /**
+        * Get a cell
+        * @param string $p
+        * @return \pq\Gateway\Cell
+        */
+       function __get($p) {
+               return $this->cell($p);
+       }
+       
        /**
         * Set a cell value
         * @param string $p
         * @param mixed $v
         */
        function __set($p, $v) {
-               $this->__get($p)->set($v);
+               $this->cell($p)->set($v);
        }
        
        /**
@@ -226,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);
        }
        
        /**
@@ -244,6 +273,7 @@ class Row implements \JsonSerializable
         * @return \pq\Gateway\Row
         */
        function create() {
+               $this->table->notify($this, "create");
                $rowset = $this->table->create($this->changes());
                if (!count($rowset)) {
                        throw new \UnexpectedValueException("No row created");
@@ -258,11 +288,9 @@ class Row implements \JsonSerializable
         * @return \pq\Gateway\Row
         */
        function update() {
-               $criteria = $this->criteria();
-               if (($lock = $this->getTable()->getLock())) {
-                       $lock->onUpdate($this, $criteria);
-               }
-               $rowset = $this->table->update($criteria, $this->changes());
+               $where = $this->criteria();
+               $this->table->notify($this, "update", $where);
+               $rowset = $this->table->update($where, $this->changes());
                if (!count($rowset)) {
                        throw new \UnexpectedValueException("No row updated");
                }
@@ -276,6 +304,7 @@ class Row implements \JsonSerializable
         * @return \pq\Gateway\Row
         */
        function delete() {
+               $this->table->notify($this, "delete");
                $rowset = $this->table->delete($this->criteria(), "*");
                if (!count($rowset)) {
                        throw new \UnexpectedValueException("No row deleted");