From e37cdc4459cca06b45dbe6101f4aa5c74a5f96c1 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Thu, 9 Dec 2021 14:57:37 +0100 Subject: [PATCH] Improvements in Entity class --- pandora_console/include/lib/Entity.php | 97 +++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 1 deletion(-) diff --git a/pandora_console/include/lib/Entity.php b/pandora_console/include/lib/Entity.php index 59306db0ca..4e7bc14153 100644 --- a/pandora_console/include/lib/Entity.php +++ b/pandora_console/include/lib/Entity.php @@ -43,6 +43,13 @@ abstract class Entity */ protected $existsInDB; + /** + * Fields to identify register. + * + * @var array + */ + protected $primaryKeys; + /** * Entity fields (from table). * @@ -126,6 +133,8 @@ abstract class Entity if (is_array($filters) === true) { // New one. + $this->primaryKeys = array_keys($filters); + $data = \db_get_row_filter( $this->table, $filters, @@ -292,8 +301,94 @@ abstract class Entity * Saves current object definition to database. * * @return boolean Success or not. + * @throws \Exception On error. */ - public abstract function save(); + public function save() + { + $updates = $this->fields; + // Clean null fields. + foreach ($updates as $k => $v) { + if ($v === null) { + unset($updates[$k]); + } + } + + if ($this->existsInDB === true) { + // Update. + $where = []; + + foreach ($this->primaryKeys as $key) { + $where[$key] = $this->fields[$key]; + } + + if (empty($where) === true) { + throw new \Exception( + __METHOD__.' error: Cannot identify object' + ); + } + + $rs = \db_process_sql_update( + $this->table, + $updates, + $where + ); + + if ($rs === false) { + global $config; + throw new \Exception( + __METHOD__.' error: '.$config['dbconnection']->error + ); + } + } else { + // New register. + $rs = \db_process_sql_insert( + $this->table, + $updates + ); + + if ($rs === false) { + global $config; + + throw new \Exception( + __METHOD__.' error: '.$config['dbconnection']->error + ); + } + + $this->existsInDB = true; + } + + return true; + + } + + + /** + * Remove this entity. + * + * @return void + * @throws \Exception If no primary keys are defined. + */ + public function delete() + { + if ($this->existsInDB === true) { + $where = []; + + foreach ($this->primaryKeys as $key) { + $where[$key] = $this->fields[$key]; + } + + if (empty($where) === true) { + throw new \Exception( + __METHOD__.' error: Cannot identify object on deletion' + ); + } + + \db_process_sql_delete( + $this->table, + $where + ); + } + } }