This commit is contained in:
2023-08-25 10:00:48 +08:00
parent 250cdda33e
commit 5caed72909
2 changed files with 10 additions and 21 deletions
+8 -19
View File
@@ -465,10 +465,13 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
return false; return false;
} }
$command = $this->getConnection()->createCommand($generate, $query->attributes); $connection = $this->getConnection()->beginTransaction();
$command = $connection->createCommand($generate, $query->attributes);
if ($command->save()) { if ($command->save()) {
$connection->commit();
return $this->refresh()->afterSave($old, $change); return $this->refresh()->afterSave($old, $change);
} else { } else {
$connection->rollback();
return FALSE; return FALSE;
} }
} }
@@ -479,16 +482,15 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
*/ */
public function save(): static|bool public function save(): static|bool
{ {
if (!$this->validator($this->rules()) || !$this->beforeSave($this)) {
return FALSE;
}
if (!$this->isNewExample) { if (!$this->isNewExample) {
if (!$this->validator($this->rules()) || !$this->beforeSave($this)) {
return FALSE;
}
[$changes, $condition] = $this->diff(); [$changes, $condition] = $this->diff();
return $this->updateInternal($condition, $condition, $changes); return $this->updateInternal($condition, $condition, $changes);
} else { } else {
return $this->create(); return $this->insert();
} }
} }
@@ -509,19 +511,6 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
/**
* @return $this|bool
* @throws Exception
*/
protected function create(): bool|static
{
if (!$this->validator($this->rules()) || !$this->beforeSave($this)) {
return FALSE;
}
return $this->insert();
}
/** /**
* @param $value * @param $value
* @return $this * @return $this
+2 -2
View File
@@ -201,8 +201,8 @@ class Connection extends Component
{ {
if ($this->storey == 0) { if ($this->storey == 0) {
/** @var PDO $pdo */ /** @var PDO $pdo */
$pdo = Context::get($this->cds); $pdo = Context::get($this->cds, $this->getTransactionClient());
if ($pdo instanceof PDO && !$this->inTransaction()) { if (!$this->inTransaction()) {
$pdo->beginTransaction(); $pdo->beginTransaction();
} }
} }