From 85371b339410a0361da28544599fae92b86ca901 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Sat, 16 Feb 2013 02:35:00 +0100 Subject: [PATCH] Support NULL values for non-string database fields --- class.databaserecord.php | 139 +++++++++++++++++++++------------------ 1 file changed, 75 insertions(+), 64 deletions(-) diff --git a/class.databaserecord.php b/class.databaserecord.php index c954061..8f59fa6 100644 --- a/class.databaserecord.php +++ b/class.databaserecord.php @@ -226,76 +226,87 @@ abstract class CPHPDatabaseRecordClass extends CPHPBaseClass $original_value = $this->uData[$column_name]; - switch($type) + if($original_value === "" && ($type == "timestamp" || $type == "numeric" || $type == "boolean")) { - case "string": - $value = htmlspecialchars(stripslashes($original_value)); - $variable_type = CPHP_VARIABLE_SAFE; - break; - case "html": - $value = filter_html(stripslashes($original_value)); - $variable_type = CPHP_VARIABLE_SAFE; - break; - case "simplehtml": - $value = filter_html_strict(stripslashes($original_value)); - $variable_type = CPHP_VARIABLE_SAFE; - break; - case "nl2br": - $value = nl2br(htmlspecialchars(stripslashes($original_value)), false); - $variable_type = CPHP_VARIABLE_SAFE; - break; - case "numeric": - $value = (is_numeric($original_value)) ? $original_value : 0; - $variable_type = CPHP_VARIABLE_SAFE; - break; - case "timestamp": - $value = unix_from_mysql($original_value); - $variable_type = CPHP_VARIABLE_SAFE; - break; - case "boolean": - $value = (empty($original_value)) ? false : true; - $variable_type = CPHP_VARIABLE_SAFE; - break; - case "none": - $value = $original_value; - $variable_type = CPHP_VARIABLE_UNSAFE; - break; - default: - $found = false; - foreach(get_object_vars($cphp_config->class_map) as $class_type => $class_name) - { - if($type == $class_type) + $variable_name_safe = "s" . $variable_name; + $this->$variable_name_safe = null; + + $variable_name_unsafe = "u" . $variable_name; + $this->$variable_name_unsafe = null; + } + else + { + switch($type) + { + case "string": + $value = htmlspecialchars(stripslashes($original_value)); + $variable_type = CPHP_VARIABLE_SAFE; + break; + case "html": + $value = filter_html(stripslashes($original_value)); + $variable_type = CPHP_VARIABLE_SAFE; + break; + case "simplehtml": + $value = filter_html_strict(stripslashes($original_value)); + $variable_type = CPHP_VARIABLE_SAFE; + break; + case "nl2br": + $value = nl2br(htmlspecialchars(stripslashes($original_value)), false); + $variable_type = CPHP_VARIABLE_SAFE; + break; + case "numeric": + $value = (is_numeric($original_value)) ? $original_value : 0; + $variable_type = CPHP_VARIABLE_SAFE; + break; + case "timestamp": + $value = unix_from_mysql($original_value); + $variable_type = CPHP_VARIABLE_SAFE; + break; + case "boolean": + $value = (empty($original_value)) ? false : true; + $variable_type = CPHP_VARIABLE_SAFE; + break; + case "none": + $value = $original_value; + $variable_type = CPHP_VARIABLE_UNSAFE; + break; + default: + $found = false; + foreach(get_object_vars($cphp_config->class_map) as $class_type => $class_name) { - try + if($type == $class_type) { - $value = new $class_name($original_value); - } - catch (NotFoundException $e) - { - $e->field = $variable_name; - throw $e; + try + { + $value = new $class_name($original_value); + } + catch (NotFoundException $e) + { + $e->field = $variable_name; + throw $e; + } + $variable_type = CPHP_VARIABLE_SAFE; + $found = true; } - $variable_type = CPHP_VARIABLE_SAFE; - $found = true; } - } - - if($found == false) - { - $classname = get_class($this); - throw new Exception("Cannot determine type of dataset ({$type}) passed on to {$classname}.BindDataset."); - break; - } - } - - if($variable_type == CPHP_VARIABLE_SAFE) - { - $variable_name_safe = "s" . $variable_name; - $this->$variable_name_safe = $value; + + if($found == false) + { + $classname = get_class($this); + throw new Exception("Cannot determine type of dataset ({$type}) passed on to {$classname}.BindDataset."); + break; + } + } + + if($variable_type == CPHP_VARIABLE_SAFE) + { + $variable_name_safe = "s" . $variable_name; + $this->$variable_name_safe = $value; + } + + $variable_name_unsafe = "u" . $variable_name; + $this->$variable_name_unsafe = $original_value; } - - $variable_name_unsafe = "u" . $variable_name; - $this->$variable_name_unsafe = $original_value; } public function FillDefaults()