Updated according to ribasushi's review.
[dbsrgits/DBIx-Class-Manual-SQLHackers.git] / lib / DBIx / Class / Manual / SQLHackers / Transactions.pod
1 =head1 NAME
2
3 DBIx::Class::Manual::SQLHackers::DELETE - DBIx::Class for SQL Hackers - DELETE
4
5 =over
6
7 =item L<Introduction|DBIx::Class::Manual::SQLHackers::Introduction>
8
9 =item L<CREATE|DBIx::Class::Manual::SQLHackers::CREATE>
10
11 =item L<INSERT|DBIx::Class::Manual::SQLHackers::INSERT>
12
13 =item L<SELECT|DBIx::Class::Manual::SQLHackers::SELECT>
14
15 =item L<UPDATE|DBIx::Class::Manual::SQLHackers::UPDATE>
16
17 =item L<DELETE|DBIx::Class::Manual::SQLHackers::DELETE>
18
19 =item BEGIN, COMMIT
20
21 =back
22
23 =head1 Transactions
24
25     BEGIN;
26     SELECT users.id, users.username FROM users WHERE id = 1;
27     UPDATE users SET username = 'fred' WHERE id = 1;
28     COMMIT;
29
30 To create a transaction, put all your changes inside a coderef, and pass it to the B<txn_do> method on the Schema object. Transactions can also be safely nested
31
32 =over
33
34 =item 1. Create a Schema object representing the database you are working with:
35
36         my $schema = MyDatabase::Schema->connect('dbi:SQLite:my.db');
37
38 =item 2. Call the B<txn_do> method on the Schema object:
39
40         $schema->txn_do( sub {
41            my $user = $schema->resultset('User')->find({ id => 1 });
42            die if(!$user);
43            $user->update({ username => 'fred' });
44         } );
45
46 =back
47