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;
}
$command = $this->getConnection()->createCommand($generate, $query->attributes);
$connection = $this->getConnection()->beginTransaction();
$command = $connection->createCommand($generate, $query->attributes);
if ($command->save()) {
$connection->commit();
return $this->refresh()->afterSave($old, $change);
} else {
$connection->rollback();
return FALSE;
}
}
@@ -479,16 +482,15 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
*/
public function save(): static|bool
{
if (!$this->validator($this->rules()) || !$this->beforeSave($this)) {
return FALSE;
}
if (!$this->isNewExample) {
if (!$this->validator($this->rules()) || !$this->beforeSave($this)) {
return FALSE;
}
[$changes, $condition] = $this->diff();
return $this->updateInternal($condition, $condition, $changes);
} 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
* @return $this