Commit 780fdd61 by Qiang Xue

Fixes #6710

parent 7b52fd5e
...@@ -420,25 +420,24 @@ class ActiveRecord extends BaseActiveRecord ...@@ -420,25 +420,24 @@ 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);
try { }
$result = $this->insertInternal($attributes);
if ($result === false) { $transaction = static::getDb()->beginTransaction();
$transaction->rollBack(); try {
} else { $result = $this->insertInternal($attributes);
$transaction->commit(); if ($result === false) {
}
} catch (\Exception $e) {
$transaction->rollBack(); $transaction->rollBack();
throw $e; } else {
$transaction->commit();
} }
} else { return $result;
$result = $this->insertInternal($attributes); } catch (\Exception $e) {
$transaction->rollBack();
throw $e;
} }
return $result;
} }
/** /**
...@@ -538,25 +537,24 @@ class ActiveRecord extends BaseActiveRecord ...@@ -538,25 +537,24 @@ 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);
try { }
$result = $this->updateInternal($attributeNames);
if ($result === false) { $transaction = static::getDb()->beginTransaction();
$transaction->rollBack(); try {
} else { $result = $this->updateInternal($attributeNames);
$transaction->commit(); if ($result === false) {
}
} catch (\Exception $e) {
$transaction->rollBack(); $transaction->rollBack();
throw $e; } else {
$transaction->commit();
} }
} else { return $result;
$result = $this->updateInternal($attributeNames); } catch (\Exception $e) {
$transaction->rollBack();
throw $e;
} }
return $result;
} }
/** /**
...@@ -580,25 +578,23 @@ class ActiveRecord extends BaseActiveRecord ...@@ -580,25 +578,23 @@ 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(); }
try {
$result = $this->deleteInternal(); $transaction = static::getDb()->beginTransaction();
if ($result === false) { try {
$transaction->rollBack(); $result = $this->deleteInternal();
} else { if ($result === false) {
$transaction->commit();
}
} catch (\Exception $e) {
$transaction->rollBack(); $transaction->rollBack();
throw $e; } else {
$transaction->commit();
} }
} else { return $result;
$result = $this->deleteInternal(); } catch (\Exception $e) {
$transaction->rollBack();
throw $e;
} }
return $result;
} }
/** /**
...@@ -609,22 +605,23 @@ class ActiveRecord extends BaseActiveRecord ...@@ -609,22 +605,23 @@ 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 }
// the record is already deleted in the database and thus the method will return 0
$condition = $this->getOldPrimaryKey(true); // we do not check the return value of deleteAll() because it's possible
$lock = $this->optimisticLock(); // the record is already deleted in the database and thus the method will return 0
if ($lock !== null) { $condition = $this->getOldPrimaryKey(true);
$condition[$lock] = $this->$lock; $lock = $this->optimisticLock();
} if ($lock !== null) {
$result = $this->deleteAll($condition); $condition[$lock] = $this->$lock;
if ($lock !== null && !$result) { }
throw new StaleObjectException('The object being deleted is outdated.'); $result = $this->deleteAll($condition);
} if ($lock !== null && !$result) {
$this->setOldAttributes(null); throw new StaleObjectException('The object being deleted is outdated.');
$this->afterDelete();
} }
$this->setOldAttributes(null);
$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