diff --git a/site/app_model.php b/site/app_model.php index 64c398b..12d1f58 100644 --- a/site/app_model.php +++ b/site/app_model.php @@ -39,6 +39,7 @@ class AppModel extends Model { var $actsAs = array('Containable', 'Linkable'); + var $useNullForEmpty = true; /** * Get Enum Values @@ -131,6 +132,32 @@ class AppModel extends Model { } + function recursive_array_replace($find, $replace, &$data) { + if (!isset($data)) + return; + + if (is_array($data)) { + foreach ($data as $key => &$value) { + $this->recursive_array_replace($find, $replace, $value); + } + return; + } + + if (isset($replace)) + $data = preg_replace($find, $replace, $data); + elseif (preg_match($find, $data)) + $data = null; + } + + function beforeSave() { + // Replace all empty strings with NULL. + // If a particular model doesn't like this, they'll have to + // override the behavior, or set useNullForEmpty to false. + if ($this->useNullForEmpty) + $this->recursive_array_replace("/^\s*$/", null, $this->data); + return true; + } + /************************************************************************** ************************************************************************** **************************************************************************