From 7eccd9d926ea6ff96a82825f5ae831a80e727c97 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Thu, 26 Dec 2013 12:31:13 -0500 Subject: [PATCH] Fixes #1641: Added `BaseActiveRecord::updateAttributes()` --- framework/CHANGELOG.md | 1 + framework/yii/db/BaseActiveRecord.php | 31 ++++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index e8e324b..152b3e9 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -31,6 +31,7 @@ Yii Framework 2 Change Log - Enh #1581: Added `ActiveQuery::joinWith()` and `ActiveQuery::innerJoinWith()` to support joining with relations (qiangxue) - Enh #1601: Added support for tagName and encodeLabel parameters in ButtonDropdown (omnilight) - Enh #1611: Added `BaseActiveRecord::markAttributeDirty()` (qiangxue) +- Enh #1641: Added `BaseActiveRecord::updateAttributes()` (qiangxue) - Enh: Added `favicon.ico` and `robots.txt` to defauly application templates (samdark) - Enh: Added `Widget::autoIdPrefix` to support prefixing automatically generated widget IDs (qiangxue) - Enh: Support for file aliases in console command 'message' (omnilight) diff --git a/framework/yii/db/BaseActiveRecord.php b/framework/yii/db/BaseActiveRecord.php index 4db9d5f..9dbcf99 100644 --- a/framework/yii/db/BaseActiveRecord.php +++ b/framework/yii/db/BaseActiveRecord.php @@ -637,7 +637,36 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface } /** - * @see CActiveRecord::update() + * Updates the specified attributes. + * + * This method is a shortcut to [[update()]] when data validation is not needed + * and only a list of attributes need to be updated. + * + * You may specify the attributes to be updated as name list or name-value pairs. + * If the latter, the corresponding attribute values will be modified accordingly. + * The method will then save the specified attributes into database. + * + * Note that this method will NOT perform data validation. + * + * @param array $attributes the attributes (names or name-value pairs) to be updated + * @return integer|boolean the number of rows affected, or false if [[beforeSave()]] stops the updating process. + */ + public function updateAttributes($attributes) + { + $attrs = []; + foreach ($attributes as $name => $value) { + if (is_integer($name)) { + $attrs[] = $value; + } else { + $this->$name = $value; + $attrs[] = $name; + } + } + return $this->update(false, $attrs); + } + + /** + * @see update() * @throws StaleObjectException */ protected function updateInternal($attributes = null)