mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-01 20:19:42 +08:00
Fix #18993: Load defaults by attributes() in yii\db\ActiveRecord::loadDefaultValues()
This commit is contained in:
@ -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
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
33
tests/data/ar/CroppedType.php
Normal file
33
tests/data/ar/CroppedType.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yiiunit\data\ar;
|
||||
|
||||
/**
|
||||
* Model representing 2 columns from "type" table.
|
||||
*
|
||||
* @property int $int_col
|
||||
* @property int $int_col2 DEFAULT 1
|
||||
*/
|
||||
class CroppedType extends ActiveRecord
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function tableName()
|
||||
{
|
||||
return '{{%type}}';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function attributes()
|
||||
{
|
||||
return ['int_col', 'int_col2'];
|
||||
}
|
||||
}
|
||||
@ -35,6 +35,7 @@ use yiiunit\data\ar\OrderWithNullFK;
|
||||
use yiiunit\data\ar\Profile;
|
||||
use yiiunit\data\ar\ProfileWithConstructor;
|
||||
use yiiunit\data\ar\Type;
|
||||
use yiiunit\data\ar\CroppedType;
|
||||
use yiiunit\framework\ar\ActiveRecordTestTrait;
|
||||
use yiiunit\framework\db\cubrid\ActiveRecordTest as CubridActiveRecordTest;
|
||||
use yiiunit\TestCase;
|
||||
@ -1344,7 +1345,6 @@ abstract class ActiveRecordTest extends DatabaseTestCase
|
||||
$this->assertEquals(1.23, $model->float_col2);
|
||||
$this->assertEquals(33.22, $model->numeric_col);
|
||||
$this->assertEquals(true, $model->bool_col2);
|
||||
|
||||
if ($this instanceof CubridActiveRecordTest) {
|
||||
// cubrid has non-standard timestamp representation
|
||||
$this->assertEquals('12:00:00 AM 01/01/2002', $model->time);
|
||||
@ -1354,15 +1354,18 @@ abstract class ActiveRecordTest extends DatabaseTestCase
|
||||
|
||||
$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);
|
||||
|
||||
// Cropped model with 2 attributes/columns
|
||||
$model = new CroppedType();
|
||||
$model->loadDefaultValues();
|
||||
$this->assertEquals(['int_col2' => 1], $model->toArray());
|
||||
}
|
||||
|
||||
public function testUnlinkAllViaTable()
|
||||
|
||||
Reference in New Issue
Block a user