Commit 780fdd61 by Qiang Xue

Fixes #6710

parent 7b52fd5e
...@@ -420,9 +420,12 @@ class ActiveRecord extends BaseActiveRecord ...@@ -420,9 +420,12 @@ class ActiveRecord extends BaseActiveRecord
Yii::info('Model not inserted due to validation error.', __METHOD__); Yii::info('Model not inserted due to validation error.', __METHOD__);
return false; return false;
} }
$db = static::getDb();
if ($this->isTransactional(self::OP_INSERT)) { if (!$this->isTransactional(self::OP_INSERT)) {
$transaction = $db->beginTransaction(); return $this->insertInternal($attributes);
}
$transaction = static::getDb()->beginTransaction();
try { try {
$result = $this->insertInternal($attributes); $result = $this->insertInternal($attributes);
if ($result === false) { if ($result === false) {
...@@ -430,15 +433,11 @@ class ActiveRecord extends BaseActiveRecord ...@@ -430,15 +433,11 @@ class ActiveRecord extends BaseActiveRecord
} else { } else {
$transaction->commit(); $transaction->commit();
} }
return $result;
} catch (\Exception $e) { } catch (\Exception $e) {
$transaction->rollBack(); $transaction->rollBack();
throw $e; throw $e;
} }
} else {
$result = $this->insertInternal($attributes);
}
return $result;
} }
/** /**
...@@ -538,9 +537,12 @@ class ActiveRecord extends BaseActiveRecord ...@@ -538,9 +537,12 @@ class ActiveRecord extends BaseActiveRecord
Yii::info('Model not updated due to validation error.', __METHOD__); Yii::info('Model not updated due to validation error.', __METHOD__);
return false; return false;
} }
$db = static::getDb();
if ($this->isTransactional(self::OP_UPDATE)) { if (!$this->isTransactional(self::OP_UPDATE)) {
$transaction = $db->beginTransaction(); return $this->updateInternal($attributeNames);
}
$transaction = static::getDb()->beginTransaction();
try { try {
$result = $this->updateInternal($attributeNames); $result = $this->updateInternal($attributeNames);
if ($result === false) { if ($result === false) {
...@@ -548,15 +550,11 @@ class ActiveRecord extends BaseActiveRecord ...@@ -548,15 +550,11 @@ class ActiveRecord extends BaseActiveRecord
} else { } else {
$transaction->commit(); $transaction->commit();
} }
return $result;
} catch (\Exception $e) { } catch (\Exception $e) {
$transaction->rollBack(); $transaction->rollBack();
throw $e; throw $e;
} }
} else {
$result = $this->updateInternal($attributeNames);
}
return $result;
} }
/** /**
...@@ -580,9 +578,11 @@ class ActiveRecord extends BaseActiveRecord ...@@ -580,9 +578,11 @@ class ActiveRecord extends BaseActiveRecord
*/ */
public function delete() public function delete()
{ {
$db = static::getDb(); if (!$this->isTransactional(self::OP_DELETE)) {
if ($this->isTransactional(self::OP_DELETE)) { return $this->deleteInternal();
$transaction = $db->beginTransaction(); }
$transaction = static::getDb()->beginTransaction();
try { try {
$result = $this->deleteInternal(); $result = $this->deleteInternal();
if ($result === false) { if ($result === false) {
...@@ -590,15 +590,11 @@ class ActiveRecord extends BaseActiveRecord ...@@ -590,15 +590,11 @@ class ActiveRecord extends BaseActiveRecord
} else { } else {
$transaction->commit(); $transaction->commit();
} }
return $result;
} catch (\Exception $e) { } catch (\Exception $e) {
$transaction->rollBack(); $transaction->rollBack();
throw $e; throw $e;
} }
} else {
$result = $this->deleteInternal();
}
return $result;
} }
/** /**
...@@ -609,8 +605,10 @@ class ActiveRecord extends BaseActiveRecord ...@@ -609,8 +605,10 @@ class ActiveRecord extends BaseActiveRecord
*/ */
protected function deleteInternal() protected function deleteInternal()
{ {
$result = false; if (!$this->beforeDelete()) {
if ($this->beforeDelete()) { return false;
}
// we do not check the return value of deleteAll() because it's possible // we do not check the return value of deleteAll() because it's possible
// the record is already deleted in the database and thus the method will return 0 // the record is already deleted in the database and thus the method will return 0
$condition = $this->getOldPrimaryKey(true); $condition = $this->getOldPrimaryKey(true);
...@@ -624,7 +622,6 @@ class ActiveRecord extends BaseActiveRecord ...@@ -624,7 +622,6 @@ class ActiveRecord extends BaseActiveRecord
} }
$this->setOldAttributes(null); $this->setOldAttributes(null);
$this->afterDelete(); $this->afterDelete();
}
return $result; return $result;
} }
......
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