From a0967588fbb83666f485e2ae549a9a9961f28fca Mon Sep 17 00:00:00 2001 From: Paolo Date: Thu, 25 Apr 2019 13:38:46 +0200 Subject: [PATCH] Better error messages in DB_Model --- application/core/DB_Model.php | 50 ++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/application/core/DB_Model.php b/application/core/DB_Model.php index 199f81162..36474f9d3 100644 --- a/application/core/DB_Model.php +++ b/application/core/DB_Model.php @@ -64,7 +64,7 @@ class DB_Model extends CI_Model public function insert($data) { // Check class properties - if (is_null($this->dbTable)) return error(EXIT_MODEL, FHC_NODBTABLE); + if (is_null($this->dbTable)) return error('The given database table name is not valid', EXIT_MODEL); // If this table has UDF and the validation of them is ok if (isError($validate = $this->_manageUDFs($data, $this->dbTable))) return $validate; @@ -114,8 +114,8 @@ class DB_Model extends CI_Model public function update($id, $data) { // Check class properties - if (is_null($this->pk)) return error(EXIT_MODEL, FHC_NOPK); - if (is_null($this->dbTable)) return error(EXIT_MODEL, FHC_NODBTABLE); + if (is_null($this->pk)) return error('The given primary key is not valid', EXIT_MODEL); + if (is_null($this->dbTable)) return error('The given database table name is not valid', EXIT_MODEL); // If this table has UDF and the validation of them is ok if (isError($validate = $this->_manageUDFs($data, $this->dbTable, $id))) return $validate; @@ -161,8 +161,8 @@ class DB_Model extends CI_Model public function delete($id) { // Check class properties - if (is_null($this->dbTable)) return error(EXIT_MODEL, FHC_NODBTABLE); - if (is_null($this->pk)) return error(EXIT_MODEL, FHC_NOPK); + if (is_null($this->pk)) return error('The given primary key is not valid', EXIT_MODEL); + if (is_null($this->dbTable)) return error('The given database table name is not valid', EXIT_MODEL); $tmpId = $id; @@ -203,8 +203,8 @@ class DB_Model extends CI_Model public function load($id = null) { // Check class properties - if (is_null($this->pk)) return error(EXIT_MODEL, FHC_NOPK); - if (is_null($this->dbTable)) return error(EXIT_MODEL, FHC_NODBTABLE); + if (is_null($this->pk)) return error('The given primary key is not valid', EXIT_MODEL); + if (is_null($this->dbTable)) return error('The given database table name is not valid', EXIT_MODEL); $tmpId = $id; @@ -232,7 +232,7 @@ class DB_Model extends CI_Model public function loadWhere($where = null) { // Check class properties - if (is_null($this->dbTable)) return error(EXIT_MODEL, FHC_NODBTABLE); + if (is_null($this->dbTable)) return error('The given database table name is not valid', EXIT_MODEL); // Execute query $result = $this->db->get_where($this->dbTable, $where); @@ -264,7 +264,7 @@ class DB_Model extends CI_Model public function loadTree($mainTable, $sideTables, $where = null, $sideTablesAliases = null) { // Check class properties - if (is_null($this->dbTable)) return error(EXIT_MODEL, FHC_NODBTABLE); + if (is_null($this->dbTable)) return error('The given database table name is not valid', EXIT_MODEL); // List of tables on which it will work $tables = array_merge(array($mainTable), $sideTables); @@ -405,12 +405,12 @@ class DB_Model extends CI_Model || is_null($cond) || !in_array($type, array('', 'LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'))) { - return error(EXIT_MODEL, EXIT_MODEL); + return error('The joining operation type is not valid', EXIT_MODEL); } $this->db->join($joinTable, $cond, $type); - return success(true); + return success(); } /** @@ -420,12 +420,13 @@ class DB_Model extends CI_Model */ public function addOrder($field = null, $type = 'ASC') { - // Check class properties and parameters - if (is_null($field) || !in_array($type, array('ASC', 'DESC'))) return error(EXIT_MODEL, EXIT_MODEL); + // Check parameters + if (is_null($field)) return error('The field parameter is not valid', EXIT_MODEL); + if (!in_array($type, array('ASC', 'DESC'))) return error('The order type is not valid', EXIT_MODEL); $this->db->order_by($field, $type); - return success(true); + return success(); } /** @@ -435,12 +436,12 @@ class DB_Model extends CI_Model */ public function addSelect($select, $escape = true) { - // Check class properties and parameters - if (is_null($select) || $select == '') return error(EXIT_MODEL, EXIT_MODEL); + // Check parameters + if (is_null($select) || $select == '') return error('The select parameter is not valid', EXIT_MODEL); $this->db->select($select, $escape); - return success(true); + return success(); } /** @@ -461,7 +462,8 @@ class DB_Model extends CI_Model public function addLimit($start = null, $end = null) { // Check class properties and parameters - if (!is_numeric($start) || (is_numeric($start) && $start <= 0)) return error(EXIT_MODEL, EXIT_MODEL); + if (!is_numeric($start) || (is_numeric($start) && $start <= 0)) + return error('The start parameter is not valid', EXIT_MODEL); if (is_numeric($end) && $end > $start) { @@ -472,7 +474,7 @@ class DB_Model extends CI_Model $this->db->limit($start); } - return success(true); + return success(); } /** @@ -485,7 +487,7 @@ class DB_Model extends CI_Model $tmpTable = trim($table); // Check parameters - if (isEmptyString($tmpTable)) return error(EXIT_MODEL, EXIT_MODEL); + if (isEmptyString($tmpTable)) return error('The table parameter is not valid', EXIT_MODEL); if (!isEmptyString($alias)) { @@ -494,7 +496,7 @@ class DB_Model extends CI_Model $this->db->from($tmpTable); - return success(true); + return success(); } /** @@ -509,12 +511,12 @@ class DB_Model extends CI_Model || (is_array($fields) && count($fields) == 0) || (is_string($fields) && $fields == '')) { - return error(EXIT_MODEL, EXIT_MODEL); + return error('The fields parameter is not valid', EXIT_MODEL); } $this->db->group_by($fields); - return success(true); + return success(); } /** @@ -827,7 +829,7 @@ class DB_Model extends CI_Model */ private function _manageUDFs(&$data, $schemaAndTable, $id = null) { - $manageUDFs = success(true); + $manageUDFs = success(); if ($this->hasUDF()) {