ensure accessing a cell on ref update
[m6w6/pq-gateway] / lib / pq / Gateway / Row.php
index bba7c09c07642c90fe1d877f18c7462da40e7bfe..cda8d4af67a0ba857a6854b4f8c262f016f5e34d 100644 (file)
@@ -65,8 +65,8 @@ class Row implements \JsonSerializable
        }
        
        /**
-        * Export current state with security sensitive data removed. You should override that, just
-        * calls export() by default.
+        * Export current state with security sensitive data removed. You should override that.
+        * Just calls export() by default.
         * @return array
         */
        function exportPublic() {
@@ -167,10 +167,6 @@ class Row implements \JsonSerializable
                                $where["$col IS"] = new QueryExpr("NULL");
                        }
                }
-               
-               if (($lock = $this->getTable()->getLock())) {
-                       $lock->criteria($this, $where);
-               }
                return $where;
        }
        
@@ -188,6 +184,18 @@ class Row implements \JsonSerializable
                return $changes;
        }
        
+       /**
+        * Cell accessor
+        * @param string $p column name
+        * @return \pq\Gateway\Cell
+        */
+       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 or parent rows
         * @param string $p
@@ -197,10 +205,7 @@ class Row implements \JsonSerializable
                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);
-               }
-               return $this->cell[$p];
+               return $this->cell($p);
        }
        
        /**
@@ -209,7 +214,7 @@ class Row implements \JsonSerializable
         * @param mixed $v
         */
        function __set($p, $v) {
-               $this->__get($p)->set($v);
+               $this->cell($p)->set($v);
        }
        
        /**
@@ -262,7 +267,11 @@ class Row implements \JsonSerializable
         * @return \pq\Gateway\Row
         */
        function update() {
-               $rowset = $this->table->update($this->criteria(), $this->changes());
+               $criteria = $this->criteria();
+               if (($lock = $this->getTable()->getLock())) {
+                       $lock->onUpdate($this, $criteria);
+               }
+               $rowset = $this->table->update($criteria, $this->changes());
                if (!count($rowset)) {
                        throw new \UnexpectedValueException("No row updated");
                }