Commit c6f4dac2 authored by Qiang Xue's avatar Qiang Xue

Refactored AR code.

parent 7303bae3
...@@ -749,21 +749,21 @@ class ActiveRecord extends Model ...@@ -749,21 +749,21 @@ class ActiveRecord extends Model
return false; return false;
} }
$db = static::getDb(); $db = static::getDb();
$transaction = $this->isTransactional(self::OP_INSERT) && $db->getTransaction() === null ? $db->beginTransaction() : null; if ($this->isTransactional(self::OP_INSERT) && $db->getTransaction() === null) {
try { $transaction = $db->beginTransaction();
$result = $this->insertInternal($attributes); try {
if ($transaction !== null) { $result = $this->insertInternal($attributes);
if ($result === false) { if ($result === false) {
$transaction->rollback(); $transaction->rollback();
} else { } else {
$transaction->commit(); $transaction->commit();
} }
} } catch (\Exception $e) {
} catch (\Exception $e) {
if ($transaction !== null) {
$transaction->rollback(); $transaction->rollback();
throw $e;
} }
throw $e; } else {
$result = $this->insertInternal($attributes);
} }
return $result; return $result;
} }
...@@ -859,21 +859,21 @@ class ActiveRecord extends Model ...@@ -859,21 +859,21 @@ class ActiveRecord extends Model
return false; return false;
} }
$db = static::getDb(); $db = static::getDb();
$transaction = $this->isTransactional(self::OP_UPDATE) && $db->getTransaction() === null ? $db->beginTransaction() : null; if ($this->isTransactional(self::OP_UPDATE) && $db->getTransaction() === null) {
try { $transaction = $db->beginTransaction();
$result = $this->updateInternal($attributes); try {
if ($transaction !== null) { $result = $this->updateInternal($attributes);
if ($result === false) { if ($result === false) {
$transaction->rollback(); $transaction->rollback();
} else { } else {
$transaction->commit(); $transaction->commit();
} }
} } catch (\Exception $e) {
} catch (\Exception $e) {
if ($transaction !== null) {
$transaction->rollback(); $transaction->rollback();
throw $e;
} }
throw $e; } else {
$result = $this->updateInternal($attributes);
} }
return $result; return $result;
} }
...@@ -1009,6 +1009,16 @@ class ActiveRecord extends Model ...@@ -1009,6 +1009,16 @@ class ActiveRecord extends Model
return $this->_oldAttributes === null; return $this->_oldAttributes === null;
} }
/**
* 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. * Initializes the object.
* This method is called at the end of the constructor. * This method is called at the end of the constructor.
...@@ -1033,16 +1043,6 @@ class ActiveRecord extends Model ...@@ -1033,16 +1043,6 @@ class ActiveRecord extends Model
$this->trigger(self::EVENT_AFTER_FIND); $this->trigger(self::EVENT_AFTER_FIND);
} }
/**
* 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. * 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, * The default implementation will trigger an [[EVENT_BEFORE_INSERT]] event when `$insert` is true,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment