ensure accessing a cell on ref update
authorMichael Wallner <mike@php.net>
Fri, 3 May 2013 12:32:40 +0000 (14:32 +0200)
committerMichael Wallner <mike@php.net>
Fri, 3 May 2013 12:32:40 +0000 (14:32 +0200)
lib/pq/Gateway/Row.php
tests/lib/pq/Gateway/CellTest.php

index 6d9c19e750b1b215da48b1b90959692f3f6c9fd8..cda8d4af67a0ba857a6854b4f8c262f016f5e34d 100644 (file)
@@ -184,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
@@ -193,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);
        }
        
        /**
@@ -205,7 +214,7 @@ class Row implements \JsonSerializable
         * @param mixed $v
         */
        function __set($p, $v) {
-               $this->__get($p)->set($v);
+               $this->cell($p)->set($v);
        }
        
        /**
index fc08ba68f5efb743ca2eca593319f52e80a1bdd0..9235a39ec81487278d4ecb79494384505f33088f 100644 (file)
@@ -50,4 +50,15 @@ class CellTest extends \PHPUnit_Framework_TestCase {
                        $this->assertEquals("$key + 123 || 'foobar' - now()", (string) $row->$key);
                }
        }
+       
+       public function testRef() {
+               $rows = $this->table->find(null, "id desc", 2);
+               $reft = new Table("reftest");
+               $refs = new Rowset($reft);
+               $refs->append($rows->seek(0)->current()->reftest()->current());
+               $refs->append($rows->seek(1)->current()->reftest()->current());
+               $refs->seek(0)->current()->test = $rows->seek(1)->current();
+               $refs->seek(1)->current()->test = $rows->seek(0)->current();
+               $refs->update();
+       }
 }