From b66427f8c79292993ce2f3cc23727c8ba1130e91 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Wed, 19 Mar 2014 18:06:20 +0400 Subject: [PATCH 1/3] Fixes #2726: ActiveRecord now fills default values on creating new instance of the model if defaults are available from DB schema --- framework/CHANGELOG.md | 1 + framework/db/ActiveRecord.php | 12 +++++++++++ tests/unit/data/ar/Type.php | 31 ++++++++++++++++++++++++++++ tests/unit/framework/db/ActiveRecordTest.php | 12 +++++++++++ 4 files changed, 56 insertions(+) create mode 100644 tests/unit/data/ar/Type.php diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 5c13470..8691948 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -146,6 +146,7 @@ Yii Framework 2 Change Log - 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 #2714: Added support for formatting time intervals relative to the current time with `yii\base\Formatter` (drenty) +- Enh #2726: ActiveRecord now fills default values on creating new instance of the model if defaults are available from DB schema (samdark) - 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 #2756: Added support for injecting custom `isEmpty` check for all validators (qiangxue) diff --git a/framework/db/ActiveRecord.php b/framework/db/ActiveRecord.php index ad895e6..1eef600 100644 --- a/framework/db/ActiveRecord.php +++ b/framework/db/ActiveRecord.php @@ -94,6 +94,18 @@ class ActiveRecord extends BaseActiveRecord const OP_ALL = 0x07; /** + * @inheritdoc + */ + public function init() + { + foreach ($this->getTableSchema()->columns as $column) { + if ($column->defaultValue) { + $this->{$column->name} = $column->defaultValue; + } + } + } + + /** * Returns the database connection used by this AR class. * By default, the "db" application component is used as the database connection. * You may override this method if you want to use a different database connection. diff --git a/tests/unit/data/ar/Type.php b/tests/unit/data/ar/Type.php new file mode 100644 index 0000000..9dce4f6 --- /dev/null +++ b/tests/unit/data/ar/Type.php @@ -0,0 +1,31 @@ +assertTrue($orders[1]['customer2']['orders2'][0]['id'] === $orders[0]['id']); $this->assertTrue($orders[1]['customer2']['orders2'][1]['id'] === $orders[1]['id']); } + + public function testDefaultValues() + { + $model = new Type(); + $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); + } } From 39b0c454e1b3d31b71ba38f581c4f5ac7ee2e8c4 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Wed, 19 Mar 2014 23:04:48 +0400 Subject: [PATCH 2/3] Default values init moved to a method that should be called manually --- framework/CHANGELOG.md | 2 +- framework/db/ActiveRecord.php | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 8691948..52c0bf1 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -146,7 +146,7 @@ Yii Framework 2 Change Log - 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 #2714: Added support for formatting time intervals relative to the current time with `yii\base\Formatter` (drenty) -- Enh #2726: ActiveRecord now fills default values on creating new instance of the model if defaults are available from DB schema (samdark) +- 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 #2735: Added support for `DateTimeInterface` in `Formatter` (ivokund) - Enh #2756: Added support for injecting custom `isEmpty` check for all validators (qiangxue) diff --git a/framework/db/ActiveRecord.php b/framework/db/ActiveRecord.php index 1eef600..c549c14 100644 --- a/framework/db/ActiveRecord.php +++ b/framework/db/ActiveRecord.php @@ -94,15 +94,18 @@ class ActiveRecord extends BaseActiveRecord const OP_ALL = 0x07; /** - * @inheritdoc + * Loads default values from database table schema + * + * @return static model instance */ - public function init() + public function loadDefaultValues() { foreach ($this->getTableSchema()->columns as $column) { if ($column->defaultValue) { $this->{$column->name} = $column->defaultValue; } } + return $this; } /** From 5c3e4fa4ba600e070ea8e6f69ad5c024dafa9819 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Wed, 19 Mar 2014 23:07:56 +0400 Subject: [PATCH 3/3] Adjusted unit tests, added description to docs --- docs/guide/active-record.md | 9 +++++++++ tests/unit/framework/db/ActiveRecordTest.php | 1 + 2 files changed, 10 insertions(+) diff --git a/docs/guide/active-record.md b/docs/guide/active-record.md index 471399a..4b74aa1 100644 --- a/docs/guide/active-record.md +++ b/docs/guide/active-record.md @@ -193,6 +193,15 @@ Customer::updateAllCounters(['age' => 1]); > 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`. +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 ------------------------- diff --git a/tests/unit/framework/db/ActiveRecordTest.php b/tests/unit/framework/db/ActiveRecordTest.php index ad4a5de..4ff83ab 100644 --- a/tests/unit/framework/db/ActiveRecordTest.php +++ b/tests/unit/framework/db/ActiveRecordTest.php @@ -491,6 +491,7 @@ class ActiveRecordTest extends DatabaseTestCase 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);