From 903f5618fecbf0741cd4f5d9eb2c9a21b5b80223 Mon Sep 17 00:00:00 2001 From: Vladimir Zbrailov Date: Sat, 1 Feb 2014 12:57:40 +0400 Subject: [PATCH 1/5] Added typecast database types into php types --- framework/CHANGELOG.md | 1 + framework/db/BaseActiveRecord.php | 3 +++ 2 files changed, 4 insertions(+) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 4df30c5fdd..47602cdaef 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: 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/BaseActiveRecord.php b/framework/db/BaseActiveRecord.php index 54d94453e4..ddddf9d4ff 100644 --- a/framework/db/BaseActiveRecord.php +++ b/framework/db/BaseActiveRecord.php @@ -1002,8 +1002,11 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface { $record = static::instantiate($row); $columns = array_flip($record->attributes()); + /* @var $schema TableSchema */ + $schema = static::getDb()->getTableSchema(static::tableName()); foreach ($row as $name => $value) { if (isset($columns[$name])) { + $value = $schema->getColumn($name)->typecast($value); $record->_attributes[$name] = $value; } else { $record->$name = $value; From d0175672733f7ec3dddbc00833b8b2eb032748fd Mon Sep 17 00:00:00 2001 From: Vladimir Zbrailov Date: Sat, 1 Feb 2014 22:31:13 +0400 Subject: [PATCH 2/5] override create --- framework/db/ActiveRecord.php | 20 ++++++++++++++++++++ framework/db/BaseActiveRecord.php | 3 --- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/framework/db/ActiveRecord.php b/framework/db/ActiveRecord.php index abe1549960..d258b2ba62 100644 --- a/framework/db/ActiveRecord.php +++ b/framework/db/ActiveRecord.php @@ -274,6 +274,26 @@ class ActiveRecord extends BaseActiveRecord return new ActiveRelation($config); } + /** + * @param array $row + * @return static + */ + 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])) { + $record->setAttribute($name, $schema->getColumn($name)->typecast($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. * diff --git a/framework/db/BaseActiveRecord.php b/framework/db/BaseActiveRecord.php index ddddf9d4ff..54d94453e4 100644 --- a/framework/db/BaseActiveRecord.php +++ b/framework/db/BaseActiveRecord.php @@ -1002,11 +1002,8 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface { $record = static::instantiate($row); $columns = array_flip($record->attributes()); - /* @var $schema TableSchema */ - $schema = static::getDb()->getTableSchema(static::tableName()); foreach ($row as $name => $value) { if (isset($columns[$name])) { - $value = $schema->getColumn($name)->typecast($value); $record->_attributes[$name] = $value; } else { $record->$name = $value; From 3319b54c8013b61ec6c343dfa0e8a146cf812961 Mon Sep 17 00:00:00 2001 From: Vladimir Zbrailov Date: Sun, 2 Feb 2014 20:20:56 +0400 Subject: [PATCH 3/5] added inherit doc block and task number --- framework/CHANGELOG.md | 2 +- framework/db/BaseActiveRecord.php | 14 +------------- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 47602cdaef..1111c9fd0b 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -115,7 +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: Added typecast database types into php types (dizews) +- 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/BaseActiveRecord.php b/framework/db/BaseActiveRecord.php index 54d94453e4..852eb7377c 100644 --- a/framework/db/BaseActiveRecord.php +++ b/framework/db/BaseActiveRecord.php @@ -984,19 +984,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface } /** - * Creates an active record object using a row of data from the database/storage. - * - * This method is *not* meant to be used to create new records. - * - * It is an internal method meant to be called to create active record objects after - * fetching data from the database. It is mainly used by [[ActiveQuery]] to populate - * the query results into Active Records. - * - * When calling this method manually you should call [[afterFind()]] on the created - * record to trigger the [[EVENT_AFTER_FIND|afterFind Event]]. - * - * @param array $row attribute values (name => value) - * @return static the newly created active record. + * @inheritdoc */ public static function create($row) { From 0e0acb2731eaa3f8c062e23cdcc84e856766dddb Mon Sep 17 00:00:00 2001 From: Vladimir Zbrailov Date: Sun, 2 Feb 2014 20:20:56 +0400 Subject: [PATCH 4/5] added inherit doc block and task number --- framework/CHANGELOG.md | 2 +- framework/db/ActiveRecord.php | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 47602cdaef..bcd96c9c13 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -115,7 +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: Added typecast database types into php types (dizews) +- 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 d258b2ba62..462804b985 100644 --- a/framework/db/ActiveRecord.php +++ b/framework/db/ActiveRecord.php @@ -275,8 +275,7 @@ class ActiveRecord extends BaseActiveRecord } /** - * @param array $row - * @return static + * @inheritdoc */ public static function create($row) { From a97f6387f9338f3830563fa6c8bc1b012e162e91 Mon Sep 17 00:00:00 2001 From: Vladimir Zbrailov Date: Sun, 2 Feb 2014 22:36:56 +0400 Subject: [PATCH 5/5] added check column --- framework/db/ActiveRecord.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/framework/db/ActiveRecord.php b/framework/db/ActiveRecord.php index 462804b985..d1aaad8e3b 100644 --- a/framework/db/ActiveRecord.php +++ b/framework/db/ActiveRecord.php @@ -284,7 +284,11 @@ class ActiveRecord extends BaseActiveRecord $schema = static::getTableSchema(); foreach ($row as $name => $value) { if (isset($columns[$name])) { - $record->setAttribute($name, $schema->getColumn($name)->typecast($value)); + if ($schema->getColumn($name) !== null) { + $record->setAttribute($name, $schema->getColumn($name)->typecast($value)); + } else { + $record->setAttribute($name, $value); + } } else { $record->$name = $value; }