diff --git a/src/Model.php b/src/Model.php index 4edae69..c50f95f 100644 --- a/src/Model.php +++ b/src/Model.php @@ -5,6 +5,7 @@ namespace Kletellier\PdoWrapper; class Model { protected mixed $values; + protected array $updated; protected string $table; protected string $pk; private Db $db; @@ -15,6 +16,7 @@ class Model $this->db = Db::getInstance(); $this->new = true; $this->values = array(); + $this->updated = array(); $this->pk = "id"; $this->table = $this->getDefaultTableName(); } @@ -45,6 +47,7 @@ class Model { $this->values = $data; $this->new = false; + $this->updated = []; return $this; } @@ -61,8 +64,18 @@ class Model $ret = true; } } else { - $query = $qb->getPreparedQuery($this->values, false); - $ret = $this->db->updateQuery($query); + if (count($this->updated) > 0) { + $data = []; + $data[$this->pk] = $this->values[$this->pk]; + foreach ($this->updated as $updatecol) { + $data[$updatecol] = $this->values[$updatecol]; + } + $query = $qb->getPreparedQuery($data, false); + $ret = $this->db->updateQuery($query); + if ($ret) { + $this->updated = []; + } + } } return $ret; } @@ -75,6 +88,9 @@ class Model public function __set($name, $value): void { $this->values[$name] = $value; + if (!$this->new && !in_array($name, $this->updated)) { + $this->updated[] = $name; + } } public function __get($name): mixed