X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fpq-gateway;a=blobdiff_plain;f=lib%2Fpq%2FGateway%2FRow.php;h=0a2820aa612b5462fd537a214d0c3bff63ef4e83;hp=885c95abd5a166b51cb35e6281aa72280e6a67f7;hb=89caabf7e3ebc62190fccba97e3bc11f7f32b58c;hpb=01bd45d05ce58796db7540d60671b8cff5d46bff diff --git a/lib/pq/Gateway/Row.php b/lib/pq/Gateway/Row.php index 885c95a..0a2820a 100644 --- a/lib/pq/Gateway/Row.php +++ b/lib/pq/Gateway/Row.php @@ -17,7 +17,7 @@ class Row implements \JsonSerializable /** * @var array */ - protected $mods = array(); + protected $cell = array(); /** * @param \pq\Gateway\Table $table @@ -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(); @@ -66,41 +66,73 @@ class Row implements \JsonSerializable return $this->data; } + /** + * Check whether the row contains modifications + * @return boolean + */ + function isDirty() { + foreach ($this->cell as $cell) { + if ($cell->isDirty()) { + return true; + } + } + return false; + } + + function refresh() { + $this->data = $this->table->find($this->criteria(), null, 1, 0)->current()->data; + $this->cell = array(); + return $this; + } + /** * Fill modified cells * @return \pq\Gateway\Row */ protected function prime() { - $this->mods = array(); + $this->cell = array(); foreach ($this->data as $key => $val) { - $this->mods[$key] = new Cell($this, $key, $val); + $this->cell[$key] = new Cell($this, $key, $val, true); } return $this; } /** * Transform data array to where criteria - * @param array $data * @return array */ protected function criteria() { $where = array(); - array_walk($this->data, function($v, $k) use (&$where) { + foreach($this->data as $k => $v) { $where["$k="] = $v; - }); + } return $where; } + /** + * Get an array of changed properties + * @return array + */ + protected function changes() { + $changes = array(); + foreach ($this->cell as $name => $cell) { + if ($cell->isDirty()) { + $changes[$name] = $cell->get(); + } + } + return $changes; + } + /** * Get a cell * @param string $p * @return \pq\Gateway\Cell */ function __get($p) { - if (!isset($this->mods[$p])) { - $this->mods[$p] = new Cell($this, $p, $this->data[$p]); + if (!isset($this->cell[$p])) { + $this->cell[$p] = new Cell($this, $p, $this->data[$p]); } - return $this->mods[$p]; + return $this->cell[$p]; } /** @@ -117,8 +149,8 @@ class Row implements \JsonSerializable * @return \pq\Gateway\Row */ function create() { - $this->data = $this->table->create($this->mods)->current()->data; - $this->mods = array(); + $this->data = $this->table->create($this->changes())->current()->data; + $this->cell = array(); return $this; } @@ -127,8 +159,8 @@ class Row implements \JsonSerializable * @return \pq\Gateway\Row */ function update() { - $this->data = $this->table->update($this->criteria(), $this->mods)->current()->data; - $this->mods = array(); + $this->data = $this->table->update($this->criteria(), $this->changes())->current()->data; + $this->cell = array(); return $this; }