diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 4df30c5fdd..bcd96c9c13 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -115,6 +115,7 @@ Yii Framework 2 Change Log - Enh: Added `yii\web\View::POS_LOAD` (qiangxue) - Enh: Added `yii\web\Response::clearOutputBuffers()` (qiangxue) - Enh: Improved `QueryBuilder::buildLimit()` to support big numbers (qiangxue) +- Enh:#2211: Added typecast database types into php types (dizews) - Enh #2240: Improved `yii\web\AssetManager::publish()`, `yii\web\AssetManager::getPublishedPath()` and `yii\web\AssetManager::getPublishedUrl()` to support aliases (vova07) - Chg #1519: `yii\web\User::loginRequired()` now returns the `Response` object instead of exiting the application (qiangxue) - Chg #1586: `QueryBuilder::buildLikeCondition()` will now escape special characters and use percentage characters by default (qiangxue) diff --git a/framework/db/ActiveRecord.php b/framework/db/ActiveRecord.php index abe1549960..d1aaad8e3b 100644 --- a/framework/db/ActiveRecord.php +++ b/framework/db/ActiveRecord.php @@ -274,6 +274,29 @@ class ActiveRecord extends BaseActiveRecord return new ActiveRelation($config); } + /** + * @inheritdoc + */ + public static function create($row) + { + $record = static::instantiate($row); + $columns = array_flip($record->attributes()); + $schema = static::getTableSchema(); + foreach ($row as $name => $value) { + if (isset($columns[$name])) { + if ($schema->getColumn($name) !== null) { + $record->setAttribute($name, $schema->getColumn($name)->typecast($value)); + } else { + $record->setAttribute($name, $value); + } + } else { + $record->$name = $value; + } + } + $record->setOldAttributes($record->getAttributes()); + return $record; + } + /** * Inserts a row into the associated database table using the attribute values of this record. *