From a7d63c8c077cb0b207e3fe16e395d3633e8e2b5f Mon Sep 17 00:00:00 2001 From: Gregory Letellier Date: Fri, 27 Oct 2023 09:23:08 +0200 Subject: [PATCH] =?UTF-8?q?sur=20update,=20ne=20faire=20l'update=20que=20s?= =?UTF-8?q?ur=20les=20colonnes=20dont=20on=20a=20affect=C3=A9e=20une=20val?= =?UTF-8?q?eur=20depuis=20le=20chargement=20de=20l'objet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Model.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) 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