From d961d2a80fb6b8495ef80fd815a80e657acd1674 Mon Sep 17 00:00:00 2001 From: Gregory Letellier Date: Tue, 18 Jun 2024 13:02:18 +0200 Subject: [PATCH] =?UTF-8?q?ajout=20model=20de=20type=20readonly=20pour=20u?= =?UTF-8?q?ne=20requ=C3=AAte=20de=20select=20uniquement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Model.php | 63 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/src/Model.php b/src/Model.php index 475fffe..5bf0da9 100644 --- a/src/Model.php +++ b/src/Model.php @@ -12,21 +12,31 @@ class Model implements JsonSerializable protected string $connection = ""; protected string $pk; private bool $new; + private bool $ro; - public function __construct() + public function __construct($ro = false) { $this->new = true; $this->values = array(); $this->updated = array(); $this->pk = "id"; + $this->ro = $ro; $this->initTable(); } private function initTable(): void { - if ($this->table == "") { - $this->table = $this->getDefaultTableName(); + if(!$this->ro) + { + if ($this->table == "") { + $this->table = $this->getDefaultTableName(); + } } + else + { + $this->table = "ReadOnlyModel"; + $this->pk = "__idpk"; + } } public function getTable(): string @@ -89,31 +99,34 @@ class Model implements JsonSerializable public function save(): bool { $ret = false; - $qb = $this->newQueryBuilder(); - if ($this->new) { - $query = $qb->getPreparedQuery($this->values); - $id = Connection::getInstance($this->connection)->insertQuery($query); - if ($id !== false) { - $this->values[$this->pk] = $id; - $this->new = false; - $ret = true; - } - } else { - if (isset($this->values[$this->pk])) { - 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 = Connection::getInstance($this->connection)->updateQuery($query); - if ($ret) { - $this->updated = []; + if(!$this->ro) + { + $qb = $this->newQueryBuilder(); + if ($this->new) { + $query = $qb->getPreparedQuery($this->values); + $id = Connection::getInstance($this->connection)->insertQuery($query); + if ($id !== false) { + $this->values[$this->pk] = $id; + $this->new = false; + $ret = true; + } + } else { + if (isset($this->values[$this->pk])) { + 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 = Connection::getInstance($this->connection)->updateQuery($query); + if ($ret) { + $this->updated = []; + } } } } - } + } return $ret; }