Commit af030c5f by Alexander Makarov

Added $skipIfSet to ActiveRecord::loadDefaultValues

parent 4b14a8c6
...@@ -96,12 +96,13 @@ class ActiveRecord extends BaseActiveRecord ...@@ -96,12 +96,13 @@ class ActiveRecord extends BaseActiveRecord
/** /**
* Loads default values from database table schema * Loads default values from database table schema
* *
* @param boolean $skipIfSet if existing value should be preserved
* @return static model instance * @return static model instance
*/ */
public function loadDefaultValues() public function loadDefaultValues($skipIfSet = true)
{ {
foreach ($this->getTableSchema()->columns as $column) { foreach ($this->getTableSchema()->columns as $column) {
if ($column->defaultValue) { if ($column->defaultValue && !($skipIfSet && $this->{$column->name} !== null)) {
$this->{$column->name} = $column->defaultValue; $this->{$column->name} = $column->defaultValue;
} }
} }
......
...@@ -505,5 +505,17 @@ class ActiveRecordTest extends DatabaseTestCase ...@@ -505,5 +505,17 @@ class ActiveRecordTest extends DatabaseTestCase
} else { } else {
$this->assertEquals('2002-01-01 00:00:00', $model->time); $this->assertEquals('2002-01-01 00:00:00', $model->time);
} }
$model = new Type();
$model->char_col2 = 'not something';
$model->loadDefaultValues();
$this->assertEquals('not something', $model->char_col2);
$model = new Type();
$model->char_col2 = 'not something';
$model->loadDefaultValues(false);
$this->assertEquals('something', $model->char_col2);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment