EN English
Version: v2.4.0

action

Run queries inside a transaction.

action($callback)
Return Value
void
Transaction support depends on the database engine. If the callback returns false, the transaction is rolled back; otherwise, it is committed automatically.
$database->action(function($database) {

	$database->insert("account", [
		"name" => "foo",
		"email" => "bar@abc.com"
	]);

	$database->delete("account", [
		"user_id" => 2312
	]);

	// If any condition fails, return false to roll back the transaction.
	if ($database->has("post", ["user_id" => 2312])) {
		return false;
	}
});

Accessing Data Outside action()

To use data outside the action() callback, declare the variable first and import it into the closure with use. Any value assigned inside the callback will then be available after the transaction finishes.
$result = "";

$database->action(function($database) use (&$result) {

    $database->insert("account", [
        "user_name" => "foo"
    ]);

    $newId = $database->id();

    $result = "Account was created with ID {$newId}.";
});

echo $result;