Skip to content

Commit 06f1cb7

Browse files
committed
nette/database 4.0 wip
1 parent 757c3e0 commit 06f1cb7

File tree

2 files changed

+159
-39
lines changed

2 files changed

+159
-39
lines changed

database/cs/transactions.texy

+79-19
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,100 @@ Transakce
44
.[perex]
55
Transakce zaručují, že se buď provedou všechny operace v rámci transakce, nebo se neprovede žádná. Jsou užitečné pro zajištění konzistence dat při složitějších operacích.
66

7+
8+
Základní použití
9+
================
10+
711
Nejjednodušší způsob použití transakcí vypadá takto:
812

913
```php
10-
$database->beginTransaction();
14+
$db->beginTransaction();
1115
try {
12-
$database->query('DELETE FROM articles WHERE id = ?', $id);
13-
$database->query('INSERT INTO audit_log', [
14-
'article_id' => $id,
15-
'action' => 'delete'
16-
]);
17-
$database->commit();
16+
$db->query('DELETE FROM articles WHERE id = ?', $id);
17+
$db->query('INSERT INTO audit_log', [
18+
'article_id' => $id,
19+
'action' => 'delete'
20+
]);
21+
$db->commit();
1822
} catch (\Exception $e) {
19-
$database->rollBack();
20-
throw $e;
23+
$db->rollBack();
24+
throw $e;
2125
}
2226
```
2327

24-
Mnohem elegantněji můžete to samé zapsat pomocí metody `transaction()`. Jako parametr přijímá callback, který vykoná v transakci. Pokud callback proběhne bez výjimky, transakce se automaticky potvrdí. Pokud dojde k výjimce, transakce se zruší (rollback) a výjimka se šíří dál.
28+
Mnohem elegantněji můžete to samé zapsat pomocí metody `transaction()`:
2529

2630
```php
27-
$database->transaction(function ($database) use ($id) {
28-
$database->query('DELETE FROM articles WHERE id = ?', $id);
29-
$database->query('INSERT INTO audit_log', [
30-
'article_id' => $id,
31-
'action' => 'delete'
32-
]);
31+
$db->transaction(function ($db) use ($id) {
32+
$db->query('DELETE FROM articles WHERE id = ?', $id);
33+
$db->query('INSERT INTO audit_log', [
34+
'article_id' => $id,
35+
'action' => 'delete'
36+
]);
3337
});
3438
```
3539

3640
Metoda `transaction()` může také vracet hodnoty:
3741

3842
```php
39-
$count = $database->transaction(function ($database) {
40-
$result = $database->query('UPDATE users SET active = ?', true);
41-
return $result->getRowCount(); // vrátí počet aktualizovaných řádků
43+
$count = $db->transaction(function ($db) {
44+
$result = $db->query('UPDATE users SET active = ?', true);
45+
return $result->getRowCount(); // vrátí počet aktualizovaných řádků
46+
});
47+
```
48+
49+
50+
Zanořené transakce .{data-version:4.0}
51+
======================================
52+
53+
Nette Database podporuje zanořování transakcí pomocí SQL savepointů. To znamená, že můžete spustit transakci uvnitř jiné transakce. Zde je jednoduchý příklad:
54+
55+
```php
56+
$db->transaction(function ($db) {
57+
// hlavní transakce
58+
$db->query('INSERT INTO users', ['name' => 'John']);
59+
60+
// vnořená transakce
61+
$db->transaction(function ($db) {
62+
$db->query('UPDATE users SET role = ?', 'admin');
63+
// pokud zde nastane chyba, vrátí se zpět jen vnořená transakce
64+
// hlavní transakce pokračuje dál
65+
});
66+
67+
// pokračování hlavní transakce
68+
$db->query('INSERT INTO user_log', ['action' => 'user created']);
4269
});
4370
```
71+
72+
.[note]
73+
Podkladový mechanismus využívá ve skutečnosti jen jednu transakci na úrovni databáze a vnořené transakce emuluje pomocí savepointů. Toto chování je stejné pro všechny databáze a je zcela transparentní.
74+
75+
76+
Auto-commit režim .{data-version:4.0}
77+
=====================================
78+
79+
Auto-commit určuje, zda se každý dotaz automaticky provede v samostatné transakci. Ve výchozím nastavení je auto-commit zapnutý, což znamená, že každý dotaz tvoří samostatnou transakci.
80+
81+
Auto-commit můžete vypnout v konfiguraci:
82+
83+
```neon
84+
database:
85+
dsn: 'mysql:host=127.0.0.1;dbname=test'
86+
user: root
87+
password: secret
88+
options:
89+
autoCommit: false # vypne auto-commit
90+
```
91+
92+
nebo v kódu:
93+
94+
```php
95+
$db->setAutoCommit(false);
96+
```
97+
98+
Při vypnutém auto-commitu se automaticky spustí nová transakce v těchto případech:
99+
- při připojení k databázi
100+
- po dokončení předchozí transakce (commit nebo rollback)
101+
102+
.[note]
103+
Pokud změníte nastavení auto-commitu během aktivní transakce, transakce se automaticky potvrdí.

database/en/transactions.texy

+80-20
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,102 @@ Transactions
22
************
33

44
.[perex]
5-
Transactions guarantee that either all operations within the transaction are executed successfully, or none are executed at all. They are essential for maintaining data consistency during more complex operations.
5+
Transactions guarantee that all operations within a transaction are either executed successfully or none of them are executed. They are useful for maintaining data consistency in more complex operations.
6+
7+
8+
Basic Usage
9+
===========
610

711
The simplest way to use transactions looks like this:
812

913
```php
10-
$database->beginTransaction();
14+
$db->beginTransaction();
1115
try {
12-
$database->query('DELETE FROM articles WHERE id = ?', $id);
13-
$database->query('INSERT INTO audit_log', [
14-
'article_id' => $id,
15-
'action' => 'delete'
16-
]);
17-
$database->commit();
16+
$db->query('DELETE FROM articles WHERE id = ?', $id);
17+
$db->query('INSERT INTO audit_log', [
18+
'article_id' => $id,
19+
'action' => 'delete'
20+
]);
21+
$db->commit();
1822
} catch (\Exception $e) {
19-
$database->rollBack();
20-
throw $e;
23+
$db->rollBack();
24+
throw $e;
2125
}
2226
```
2327

24-
A much cleaner and more elegant way to achieve the same result is by using the `transaction()` method. This method accepts a callback as a parameter, which it executes within the transaction. If the callback runs without throwing an exception, the transaction is automatically committed. If an exception is thrown, the transaction is rolled back, and the exception is propagated further.
28+
You can achieve the same result more elegantly with the `transaction()` method:
2529

2630
```php
27-
$database->transaction(function ($database) use ($id) {
28-
$database->query('DELETE FROM articles WHERE id = ?', $id);
29-
$database->query('INSERT INTO audit_log', [
30-
'article_id' => $id,
31-
'action' => 'delete'
32-
]);
31+
$db->transaction(function ($db) use ($id) {
32+
$db->query('DELETE FROM articles WHERE id = ?', $id);
33+
$db->query('INSERT INTO audit_log', [
34+
'article_id' => $id,
35+
'action' => 'delete'
36+
]);
3337
});
3438
```
3539

3640
The `transaction()` method can also return values:
3741

3842
```php
39-
$count = $database->transaction(function ($database) {
40-
$result = $database->query('UPDATE users SET active = ?', true);
41-
return $result->getRowCount(); // returns the number of rows updated
43+
$count = $db->transaction(function ($db) {
44+
$result = $db->query('UPDATE users SET active = ?', true);
45+
return $result->getRowCount(); // returns the number of rows updated
46+
});
47+
```
48+
49+
50+
Nested Transactions .{data-version:4.0}
51+
=======================================
52+
53+
Nette Database supports nested transactions using SQL savepoints. This means you can start a transaction inside another transaction. Here's a simple example:
54+
55+
```php
56+
$db->transaction(function ($db) {
57+
// Main transaction
58+
$db->query('INSERT INTO users', ['name' => 'John']);
59+
60+
// Nested transaction
61+
$db->transaction(function ($db) {
62+
$db->query('UPDATE users SET role = ?', 'admin');
63+
// If an error occurs here, only the nested transaction will be rolled back,
64+
// while the main transaction will continue.
65+
});
66+
67+
// Continuing the main transaction
68+
$db->query('INSERT INTO user_log', ['action' => 'user created']);
4269
});
4370
```
71+
72+
.[note]
73+
Internally, only a single database transaction is used, and nested transactions are simulated using savepoints. This behavior is consistent across all databases and is completely transparent to the developer.
74+
75+
76+
Auto-commit Mode .{data-version:4.0}
77+
====================================
78+
79+
Auto-commit determines whether each query is automatically executed in a separate transaction. By default, auto-commit is enabled, meaning every query forms a separate transaction.
80+
81+
You can disable auto-commit in the configuration:
82+
83+
```neon
84+
database:
85+
dsn: 'mysql:host=127.0.0.1;dbname=test'
86+
user: root
87+
password: secret
88+
options:
89+
autoCommit: false # disables auto-commit
90+
```
91+
92+
Or in the code:
93+
94+
```php
95+
$db->setAutoCommit(false);
96+
```
97+
98+
When auto-commit is disabled, a new transaction automatically starts in the following cases:
99+
- When connecting to the database.
100+
- After the previous transaction is completed (commit or rollback).
101+
102+
.[note]
103+
If you change the auto-commit setting while a transaction is active, the current transaction will be automatically committed to maintain consistency.

0 commit comments

Comments
 (0)