Skip to content

Commit

Permalink
Split out the default and initial_default concepts
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaac Connor committed Oct 26, 2024
1 parent 1c0e03b commit 94c2816
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions web/includes/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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,
Expand All @@ -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 {
Expand Down

0 comments on commit 94c2816

Please sign in to comment.