From 94c2816841c54e162ecbef0f0dabd5fcca18b11f Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Sat, 26 Oct 2024 13:59:59 -0400 Subject: [PATCH] Split out the default and initial_default concepts --- web/includes/Object.php | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/web/includes/Object.php b/web/includes/Object.php index df2588a46e..b1587f01dd 100644 --- a/web/includes/Object.php +++ b/web/includes/Object.php @@ -325,14 +325,7 @@ public function changes($new_values, $defaults=null) { return $changes; } # end public function changes - public function save($new_values = null) { - $class = get_class($this); - $table = $class::$table; - - if ($new_values) { - $this->set($new_values); - } - + public function apply_defaults() { # Set defaults. Note that we only replace "" with null, not other values # because for example if we want to clear TimestampFormat, we clear it, but the default is a string value foreach ( $this->defaults as $field => $default ) { @@ -343,8 +336,31 @@ public function save($new_values = null) { } else { $this->$field = $default; } + } else { + Debug("Property $field exists ".$this->$field); + } + } # end foreach default + } + + public function apply_initial_defaults() { + # Set defaults. Note that we only replace "" with null, not other values + # because for example if we want to clear TimestampFormat, we clear it, but the default is a string value + foreach ( $this->defaults as $field => $default ) { + if (is_array($default) and isset($default['initial_default'])) { + $this->$field = $default['initial_default']; } } # end foreach default + } + + public function save($new_values = null) { + $class = get_class($this); + $table = $class::$table; + + if ($new_values) { + $this->set($new_values); + } + + $this->apply_defaults(); $fields = array_filter( $this->defaults, @@ -362,7 +378,7 @@ function($v) { if ( $this->Id() ) { $fields = array_keys($fields); $sql = 'UPDATE `'.$table.'` SET '.implode(', ', array_map(function($field) {return '`'.$field.'`=?';}, $fields)).' WHERE Id=?'; - $values = array_map(function($field){ return $this->{$field};}, $fields); + $values = array_map(function($field){ return $this->$field;}, $fields); $values[] = $this->{'Id'}; if (dbQuery($sql, $values)) return true; } else {