X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fpq-gateway;a=blobdiff_plain;f=lib%2Fpq%2FGateway%2FRow.php;h=5c0efa4ac19a30da22c3bb7627d8ffb4bd58ada5;hp=dc07e3f208e200cd0c1b87290619587d25d41882;hb=20d2b6bcce8f1c7a1aaa375b86ffb5be30674956;hpb=0e684b0140154ebc8a304670c0349da840a16ad2 diff --git a/lib/pq/Gateway/Row.php b/lib/pq/Gateway/Row.php index dc07e3f..5c0efa4 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 @@ -66,35 +66,53 @@ 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; + } + /** * 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->mods as $name => $cell) { - $changes[$name] = $cell->get(); + foreach ($this->cell as $name => $cell) { + if ($cell->isDirty()) { + $changes[$name] = $cell->get(); + } } return $changes; } @@ -105,10 +123,10 @@ class Row implements \JsonSerializable * @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]; } /** @@ -126,7 +144,7 @@ class Row implements \JsonSerializable */ function create() { $this->data = $this->table->create($this->changes())->current()->data; - $this->mods = array(); + $this->cell = array(); return $this; } @@ -136,7 +154,7 @@ class Row implements \JsonSerializable */ function update() { $this->data = $this->table->update($this->criteria(), $this->changes())->current()->data; - $this->mods = array(); + $this->cell = array(); return $this; }