version: 2.1.12
action
Start a transaction.
action($callback)
callback [function]
The transaction wrap for executing queries.
Return: void
Not every database engine supports transactions. You have to check before using it. All queries will be automatically committed inside the transaction wrap. You can also return the false value to roll back the transactions.
$database->action(function($database) { $database->insert("account", [ "name" => "foo", "email" => "bar@abc.com" ]); $database->delete("account", [ "user_id" => 2312 ]); // If you found something wrong, just return a false value to roll back the whole transaction. if ($database->has("post", ["user_id" => 2312])) { return false; } });
Accessing data outside of action
Create a result variable and refer to the action callback with the keyword `use`, and you can get data back after when you assign it from inside.
$result = ""; $database->action(function($database) use (&$result) { $database->insert("account", [ "user_name" => "foo" ]); $newId = $database->id(); $result = "Account is created, and the id is {$newId}."; }); echo $result;