mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-15 22:09:48 +08:00
Merge pull request #2815 from yiisoft/ar-default-values
Fixes #2726: ActiveRecord now fills default values on creating new instance of the model if defaults are available from DB schema
This commit is contained in:
@@ -198,6 +198,15 @@ Customer::updateAllCounters(['age' => 1]);
|
|||||||
> Info: The `save()` method will either perform an `INSERT` or `UPDATE` SQL statement, depending
|
> Info: The `save()` method will either perform an `INSERT` or `UPDATE` SQL statement, depending
|
||||||
on whether the ActiveRecord being saved is new or not by checking `ActiveRecord::isNewRecord`.
|
on whether the ActiveRecord being saved is new or not by checking `ActiveRecord::isNewRecord`.
|
||||||
|
|
||||||
|
In order to load default values from database schema you may call `loadDefaultValues()` method:
|
||||||
|
|
||||||
|
```php
|
||||||
|
$customer = new Customer();
|
||||||
|
$customer->loadDefaultValues();
|
||||||
|
$cusomer->name = 'Alexander';
|
||||||
|
$customer->save();
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Data Input and Validation
|
Data Input and Validation
|
||||||
-------------------------
|
-------------------------
|
||||||
|
|||||||
@@ -146,6 +146,7 @@ Yii Framework 2 Change Log
|
|||||||
- Enh #2661: Added boolean column type support for SQLite (qiangxue)
|
- Enh #2661: Added boolean column type support for SQLite (qiangxue)
|
||||||
- Enh #2670: Changed `console\Controller::globalOptions()` to `options($actionId)` to (make it possible to) differentiate options per action (hqx)
|
- Enh #2670: Changed `console\Controller::globalOptions()` to `options($actionId)` to (make it possible to) differentiate options per action (hqx)
|
||||||
- Enh #2714: Added support for formatting time intervals relative to the current time with `yii\base\Formatter` (drenty)
|
- Enh #2714: Added support for formatting time intervals relative to the current time with `yii\base\Formatter` (drenty)
|
||||||
|
- Enh #2726: Added `yii\db\ActiveRecord::loadDefaultValues()` that fills default values from DB schema (samdark)
|
||||||
- Enh #2729: Added `FilterValidator::skipOnArray` so that filters like `trim` will not fail for array inputs (qiangxue)
|
- Enh #2729: Added `FilterValidator::skipOnArray` so that filters like `trim` will not fail for array inputs (qiangxue)
|
||||||
- Enh #2735: Added support for `DateTimeInterface` in `Formatter` (ivokund)
|
- Enh #2735: Added support for `DateTimeInterface` in `Formatter` (ivokund)
|
||||||
- Enh #2756: Added support for injecting custom `isEmpty` check for all validators (qiangxue)
|
- Enh #2756: Added support for injecting custom `isEmpty` check for all validators (qiangxue)
|
||||||
|
|||||||
@@ -93,6 +93,21 @@ class ActiveRecord extends BaseActiveRecord
|
|||||||
*/
|
*/
|
||||||
const OP_ALL = 0x07;
|
const OP_ALL = 0x07;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads default values from database table schema
|
||||||
|
*
|
||||||
|
* @return static model instance
|
||||||
|
*/
|
||||||
|
public function loadDefaultValues()
|
||||||
|
{
|
||||||
|
foreach ($this->getTableSchema()->columns as $column) {
|
||||||
|
if ($column->defaultValue) {
|
||||||
|
$this->{$column->name} = $column->defaultValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the database connection used by this AR class.
|
* Returns the database connection used by this AR class.
|
||||||
* By default, the "db" application component is used as the database connection.
|
* By default, the "db" application component is used as the database connection.
|
||||||
|
|||||||
31
tests/unit/data/ar/Type.php
Normal file
31
tests/unit/data/ar/Type.php
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace yiiunit\data\ar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Model representing tbl_type table
|
||||||
|
*
|
||||||
|
* @property int $int_col
|
||||||
|
* @property int $int_col2 DEFAULT 1
|
||||||
|
* @property string $char_col
|
||||||
|
* @property string $char_col2 DEFAULT 'something'
|
||||||
|
* @property string $char_col3
|
||||||
|
* @property float $float_col
|
||||||
|
* @property float $float_col2 DEFAULT '1.23'
|
||||||
|
* @property string $blob_col
|
||||||
|
* @property float $numeric_col DEFAULT '33.22'
|
||||||
|
* @property string $time DEFAULT '2002-01-01 00:00:00'
|
||||||
|
* @property boolean $bool_col
|
||||||
|
* @property boolean $bool_col2 DEFAULT 1
|
||||||
|
*/
|
||||||
|
class Type extends ActiveRecord
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
public static function tableName()
|
||||||
|
{
|
||||||
|
return 'tbl_type';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -8,6 +8,7 @@ use yiiunit\data\ar\OrderItem;
|
|||||||
use yiiunit\data\ar\Order;
|
use yiiunit\data\ar\Order;
|
||||||
use yiiunit\data\ar\Item;
|
use yiiunit\data\ar\Item;
|
||||||
use yiiunit\data\ar\Profile;
|
use yiiunit\data\ar\Profile;
|
||||||
|
use yiiunit\data\ar\Type;
|
||||||
use yiiunit\framework\ar\ActiveRecordTestTrait;
|
use yiiunit\framework\ar\ActiveRecordTestTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -486,4 +487,16 @@ class ActiveRecordTest extends DatabaseTestCase
|
|||||||
$this->assertTrue($orders[1]['customer2']['orders2'][0]['id'] === $orders[0]['id']);
|
$this->assertTrue($orders[1]['customer2']['orders2'][0]['id'] === $orders[0]['id']);
|
||||||
$this->assertTrue($orders[1]['customer2']['orders2'][1]['id'] === $orders[1]['id']);
|
$this->assertTrue($orders[1]['customer2']['orders2'][1]['id'] === $orders[1]['id']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testDefaultValues()
|
||||||
|
{
|
||||||
|
$model = new Type();
|
||||||
|
$model->loadDefaultValues();
|
||||||
|
$this->assertEquals(1, $model->int_col2);
|
||||||
|
$this->assertEquals('something', $model->char_col2);
|
||||||
|
$this->assertEquals(1.23, $model->float_col2);
|
||||||
|
$this->assertEquals(33.22, $model->numeric_col);
|
||||||
|
$this->assertEquals('2002-01-01 00:00:00', $model->time);
|
||||||
|
$this->assertEquals(true, $model->bool_col2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user