Commit | Line | Data |
1ffc68a9 |
1 | ## Removed from UPDATE.pod: |
2 | |
1ffc68a9 |
3 | =head2 Update a row based on data in other tables |
4 | |
5 | -- Slightly less pointless example |
6 | UPDATE posts |
7 | SET title = user.username || title |
8 | JOIN users user ON user.id = posts.user_id; |
9 | |
10 | Joining two tables for an update is a similar sort of exercise to |
11 | joining them for a select query and using data from both. |
12 | |
13 | =over |
14 | |
15 | =item 1. Create a Schema object representing the database you are working with: |
16 | |
17 | my $schema = MyDatabase::Schema->connect('dbi:SQLite:my.db'); |
18 | |
19 | =item 2. Call the B<search> method on the resultset for the L<ResultSource|DBIx::Class::ResultSource> you wish to update data in, joining to the second table: |
20 | |
21 | my $posts = $schema->resultset('Post')->search( |
22 | {}, |
23 | { join => 'user' } |
24 | ); |
25 | |
26 | The B<join> key takes as an argument a nested structure of one or more relation names (see L<DBIx::Class::Manual::SQLHackers::CREATE>). |
27 | |
28 | =item 3. Call the B<update> method on the resultset to run the UPDATE statement: |
29 | |
30 | $posts->update({ 'me.title' => \[ 'user.username || me.title' ] }); |
31 | |
32 | ^^ I am 95% sure this won't actually work, please try it (ideally as a passing or failing test) |
33 | |
34 | =back |