Get Started

Upgrade

Where Syntax

Query

Aggregation

Fetch

Transaction

Management

Advanced

Raw object

PDO object

Debug

Information

version: 2.1.12

action

Start a transaction.

action($callback)
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;