Commit 780fdd61 by Qiang Xue

Fixes #6710

parent 7b52fd5e
......@@ -420,9 +420,12 @@ class ActiveRecord extends BaseActiveRecord
Yii::info('Model not inserted due to validation error.', __METHOD__);
return false;
}
$db = static::getDb();
if ($this->isTransactional(self::OP_INSERT)) {
$transaction = $db->beginTransaction();
if (!$this->isTransactional(self::OP_INSERT)) {
return $this->insertInternal($attributes);
}
$transaction = static::getDb()->beginTransaction();
try {
$result = $this->insertInternal($attributes);
if ($result === false) {
......@@ -430,15 +433,11 @@ class ActiveRecord extends BaseActiveRecord
} else {
$transaction->commit();
}
return $result;
} catch (\Exception $e) {
$transaction->rollBack();
throw $e;
}
} else {
$result = $this->insertInternal($attributes);
}
return $result;
}
/**
......@@ -538,9 +537,12 @@ class ActiveRecord extends BaseActiveRecord
Yii::info('Model not updated due to validation error.', __METHOD__);
return false;
}
$db = static::getDb();
if ($this->isTransactional(self::OP_UPDATE)) {
$transaction = $db->beginTransaction();
if (!$this->isTransactional(self::OP_UPDATE)) {
return $this->updateInternal($attributeNames);
}
$transaction = static::getDb()->beginTransaction();
try {
$result = $this->updateInternal($attributeNames);
if ($result === false) {
......@@ -548,15 +550,11 @@ class ActiveRecord extends BaseActiveRecord
} else {
$transaction->commit();
}
return $result;
} catch (\Exception $e) {
$transaction->rollBack();
throw $e;
}
} else {
$result = $this->updateInternal($attributeNames);
}
return $result;
}
/**
......@@ -580,9 +578,11 @@ class ActiveRecord extends BaseActiveRecord
*/
public function delete()
{
$db = static::getDb();
if ($this->isTransactional(self::OP_DELETE)) {
$transaction = $db->beginTransaction();
if (!$this->isTransactional(self::OP_DELETE)) {
return $this->deleteInternal();
}
$transaction = static::getDb()->beginTransaction();
try {
$result = $this->deleteInternal();
if ($result === false) {
......@@ -590,15 +590,11 @@ class ActiveRecord extends BaseActiveRecord
} else {
$transaction->commit();
}
return $result;
} catch (\Exception $e) {
$transaction->rollBack();
throw $e;
}
} else {
$result = $this->deleteInternal();
}
return $result;
}
/**
......@@ -609,8 +605,10 @@ class ActiveRecord extends BaseActiveRecord
*/
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);
......@@ -624,7 +622,6 @@ class ActiveRecord extends BaseActiveRecord
}
$this->setOldAttributes(null);
$this->afterDelete();
}
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