From c6f4dac2497d78269061e26077cba4af7bc4ffb6 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Wed, 11 Sep 2013 13:41:47 -0400 Subject: [PATCH] Refactored AR code. --- framework/yii/db/ActiveRecord.php | 52 +++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/framework/yii/db/ActiveRecord.php b/framework/yii/db/ActiveRecord.php index 35d7305..a09c60a 100644 --- a/framework/yii/db/ActiveRecord.php +++ b/framework/yii/db/ActiveRecord.php @@ -749,21 +749,21 @@ class ActiveRecord extends Model return false; } $db = static::getDb(); - $transaction = $this->isTransactional(self::OP_INSERT) && $db->getTransaction() === null ? $db->beginTransaction() : null; - try { - $result = $this->insertInternal($attributes); - if ($transaction !== null) { + if ($this->isTransactional(self::OP_INSERT) && $db->getTransaction() === null) { + $transaction = $db->beginTransaction(); + try { + $result = $this->insertInternal($attributes); if ($result === false) { $transaction->rollback(); } else { $transaction->commit(); } - } - } catch (\Exception $e) { - if ($transaction !== null) { + } catch (\Exception $e) { $transaction->rollback(); + throw $e; } - throw $e; + } else { + $result = $this->insertInternal($attributes); } return $result; } @@ -859,21 +859,21 @@ class ActiveRecord extends Model return false; } $db = static::getDb(); - $transaction = $this->isTransactional(self::OP_UPDATE) && $db->getTransaction() === null ? $db->beginTransaction() : null; - try { - $result = $this->updateInternal($attributes); - if ($transaction !== null) { + if ($this->isTransactional(self::OP_UPDATE) && $db->getTransaction() === null) { + $transaction = $db->beginTransaction(); + try { + $result = $this->updateInternal($attributes); if ($result === false) { $transaction->rollback(); } else { $transaction->commit(); } - } - } catch (\Exception $e) { - if ($transaction !== null) { + } catch (\Exception $e) { $transaction->rollback(); + throw $e; } - throw $e; + } else { + $result = $this->updateInternal($attributes); } return $result; } @@ -1010,6 +1010,16 @@ class ActiveRecord extends Model } /** + * Sets the value indicating whether the record is new. + * @param boolean $value whether the record is new and should be inserted when calling [[save()]]. + * @see getIsNewRecord + */ + public function setIsNewRecord($value) + { + $this->_oldAttributes = $value ? null : $this->_attributes; + } + + /** * Initializes the object. * This method is called at the end of the constructor. * The default implementation will trigger an [[EVENT_INIT]] event. @@ -1034,16 +1044,6 @@ class ActiveRecord extends Model } /** - * Sets the value indicating whether the record is new. - * @param boolean $value whether the record is new and should be inserted when calling [[save()]]. - * @see getIsNewRecord - */ - public function setIsNewRecord($value) - { - $this->_oldAttributes = $value ? null : $this->_attributes; - } - - /** * This method is called at the beginning of inserting or updating a record. * The default implementation will trigger an [[EVENT_BEFORE_INSERT]] event when `$insert` is true, * or an [[EVENT_BEFORE_UPDATE]] event if `$insert` is false.