From eb545894dfcfb086211a4438574a33d8695ed93e Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Wed, 13 Jun 2007 23:05:49 +0000 Subject: [PATCH] Modifying the error handling system in BSApi so that errors generated from validating data that's only used to fetch are ignored --- Api.php | 63 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 12 deletions(-) diff --git a/Api.php b/Api.php index ebc83a3..d193f71 100644 --- a/Api.php +++ b/Api.php @@ -116,7 +116,7 @@ abstract class BSApi public $insertid = 0; /** - * Error list that has been generated + * Error queue that builds up errors * @var array */ private $errors = array(); @@ -132,14 +132,27 @@ abstract class BSApi // ################################################################### /** - * Constructs an error for the error handler to receive + * Adds an error into the error queue that is then hit * * @param string Error message */ protected function _error($message) { $this->errors[] = $message; - + } + + // ################################################################### + /** + * This runs through all the errors in the error queue and processes + * them all at once. Error builders then get a list of all the errors, + * and die-by-hit callbacks stop at the first one + * + * @param string A string param + * + * @return integer Return value + */ + private function _processErrorQueue() + { // we want to explicitly specify silence if (APIError() == 'silent') { @@ -151,7 +164,10 @@ abstract class BSApi trigger_error('No APIError() handler has been set'); } - call_user_func(APIError(), $message); + foreach ($this->errors AS $e) + { + call_user_func(APIError(), $e); + } } // ################################################################### @@ -208,11 +224,11 @@ abstract class BSApi { if ($verify === false) { - $this->error(sprintf(_('Validation of "%1$s" failed'), $field)); + $this->_error(sprintf(_('Validation of "%1$s" failed'), $field)); } else { - $this->error($verify); + $this->_error($verify); } } } @@ -301,6 +317,8 @@ abstract class BSApi * * @param bool Run pre_fetch()? * @param bool Run post_fetch()? + * + * @return boolean Whether or not the row was successfully fetched */ public function fetch($doPre = true, $doPost = true) { @@ -309,13 +327,15 @@ abstract class BSApi $this->setCondition(); } + // reset the error queue due to any validation errors caused by fetchable fields + $this->errors = array(); + $this->_runActionMethod('pre_fetch', $doPre); $result = BSRegister::GetType('Db')->queryFirst("SELECT * FROM {$this->prefix}{$this->table} WHERE {$this->condition}"); if (!$result) { - $this->error(_('No records were returned')); - return; + return false; } $this->_runActionMethod('post_fetch', $doPost); @@ -332,6 +352,8 @@ abstract class BSApi } } } + + return true; } // ################################################################### @@ -344,6 +366,7 @@ abstract class BSApi public function insert($doPre = true, $doPost = true) { $this->verify(); + $this->_processErrorQueue(); $this->_runActionMethod('pre_insert', $doPre); @@ -389,7 +412,9 @@ abstract class BSApi { $this->setCondition(); } - + + $this->_processErrorQueue(); + $this->_runActionMethod('pre_update', $doPre); foreach ($this->setfields AS $field) @@ -438,7 +463,7 @@ abstract class BSApi { if (!isset($this->values["$name"])) { - $this->error(sprintf(_('The required field "%1$s" was not set'), $name)); + $this->_error(sprintf(_('The required field "%1$s" was not set'), $name)); } } else if ($options[F_REQ] == REQ_SET) @@ -523,6 +548,22 @@ abstract class BSApi return true; } + + // ################################################################### + /** + * Verify field: unique + */ + /*protected function verify_unique($field) + { + $res = BSRegister::GetType('Db')->queryFirst("SELECT $field FROM {$this->prefix}{$this->table} WHERE $field = " . $this->_prepareFieldForSql($field) . (empty($this->condition) ? "" : " AND !({$this->condition})")); + var_dump($res); + if ($res) + { + return sprintf(_('The "%1$s" field must contain a unique value'), $field); + } + + return true; + }*/ } // ################################################################### @@ -530,8 +571,6 @@ abstract class BSApi * Setter and getter method for the API error reporting system. Passing * a name will cause the set, no arguments will cause the get. * -* @access public -* * @param mixed Method name in callable form * * @return mixed Method name in callable form -- 2.43.5