-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransaction-usage.php
More file actions
33 lines (27 loc) · 1.09 KB
/
transaction-usage.php
File metadata and controls
33 lines (27 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
use Neuron\Database\DatabaseManager;
use Neuron\Database\Drivers\MysqlDatabaseDriver;
use Neuron\Extensibility\ExtensionStore;
use Neuron\Extensibility\InstantiatorInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
/** @var LoggerInterface $logger */
/** @var InstantiatorInterface $instantiator */
/** @var EventDispatcherInterface $eventDispatcher */
$extensions = new ExtensionStore($instantiator, $logger, $eventDispatcher);
$database = new DatabaseManager($logger, $extensions, $eventDispatcher);
$database->connect('_default_', MysqlDatabaseDriver::class, [
'database' => 'my_database',
'host' => 'localhost',
'username' => 'user',
'password' => 'password',
]);
try {
$database->transaction(function ($db) {
$db->execute('INSERT INTO accounts (name, balance) VALUES (?, ?)', ['Checking', 500]);
$db->execute('INSERT INTO transactions (account_id, amount) VALUES (?, ?)', [1, 500]);
});
echo "Transaction completed successfully.\n";
} catch (Throwable $e) {
echo "Transaction failed: " . $e->getMessage();
}