Fix #18993: Load defaults by attributes() in yii\db\ActiveRecord::loadDefaultValues()

This commit is contained in:
Anton
2021-11-12 00:25:08 +03:00
committed by GitHub
parent 533b569005
commit c8d027f649
4 changed files with 47 additions and 6 deletions

View File

@ -28,6 +28,7 @@ Yii Framework 2 Change Log
- Bug #18909: Fix bug with binding default action parameters for controllers (bizley)
- Bug #18955: Check `yiisoft/yii2-swiftmailer` before using as default mailer in `yii\base\Application` (WinterSilence)
- Bug #18988: Fix default value of `yii\console\controllers\MessageController::$translator` (WinterSilence)
- Bug #18993: Load defaults by `attributes()` in `yii\db\ActiveRecord::loadDefaultValues()` (WinterSilence)
2.0.43 August 09, 2021

View File

@ -115,9 +115,13 @@ class ActiveRecord extends BaseActiveRecord
*/
public function loadDefaultValues($skipIfSet = true)
{
foreach (static::getTableSchema()->columns as $column) {
if ($column->defaultValue !== null && (!$skipIfSet || $this->{$column->name} === null)) {
$this->{$column->name} = $column->defaultValue;
$columns = static::getTableSchema()->columns;
foreach ($this->attributes() as $name) {
if (isset($columns[$name])) {
$defaultValue = $columns[$name]->defaultValue;
if ($defaultValue !== null && (!$skipIfSet || $this->getAttribute($name) === null)) {
$this->setAttribute($name, $defaultValue);
}
}
}