Commit | Line | Data |
3b44ccc6 |
1 | =head1 NAME |
9c82c181 |
2 | |
2913b2d3 |
3 | DBIx::Class::Manual::Cookbook - Misc recipes |
ee38fa40 |
4 | |
2913b2d3 |
5 | =head1 DESCRIPTION |
6 | |
7 | Things that could be handy |
8 | |
9 | =head1 RECIPES |
ee38fa40 |
10 | |
11 | =item Input validation. |
12 | |
13 | =item Using joins |
14 | |
15 | =item Many-to-many relationships |
16 | |
17 | This is not as easy as it could be, but it's possible. Here's an example to |
18 | illustrate: |
19 | |
dfeba824 |
20 | # Set up inherited connection information |
21 | package MyApp::DBIC; |
dfeba824 |
22 | use base qw/DBIx::Class/; |
ee38fa40 |
23 | |
dfeba824 |
24 | __PACKAGE__->load_components(qw/PK::Auto::SQLite Core DB/); |
25 | __PACKAGE__->connection(...); |
ee38fa40 |
26 | |
dfeba824 |
27 | # Set up a class for the 'authors' table |
28 | package MyApp::DBIC::Author; |
29 | use base qw/MyApp::DBIC/; |
ee38fa40 |
30 | |
dfeba824 |
31 | __PACKAGE__->table('authors'); |
32 | __PACKAGE__->add_columns(qw/authID first_name last_name/); |
33 | __PACKAGE__->set_primary_key(qw/authID/); |
ee38fa40 |
34 | |
dfeba824 |
35 | # Define relationship to the link table |
36 | __PACKAGE__->has_many('b2a' => 'MyApp::DBIC::Book2Author', 'authID'); |
ee38fa40 |
37 | |
dfeba824 |
38 | # Create the accessor for books from the ::Author class |
39 | sub books { |
40 | my ($self) = @_; |
41 | return MyApp::DBIC::Book->search( |
8ab99068 |
42 | { 'b2a.authID' => $self->authID }, # WHERE clause |
43 | { join => 'b2a' } # join condition (part of search attrs) |
dfeba824 |
44 | # 'b2a' refers to the relationship named earlier in the Author class. |
45 | # 'b2a.authID' refers to the authID column of the b2a relationship, |
46 | # which becomes accessible in the search by being joined. |
8ab99068 |
47 | ); |
dfeba824 |
48 | } |
ee38fa40 |
49 | |
dfeba824 |
50 | # define the link table class |
51 | package MyApp::DBIC::Book2Author; |
dfeba824 |
52 | use base qw/MyApp::DBIC/; |
ee38fa40 |
53 | |
dfeba824 |
54 | __PACKAGE__->table('book2author'); |
55 | __PACKAGE__->add_columns(qw/bookID authID/); |
56 | __PACKAGE__->set_primary_key(qw/bookID authID/); |
ee38fa40 |
57 | |
dfeba824 |
58 | __PACKAGE__->belongs_to('authID' => 'MyApp::DBIC::Author'); |
59 | __PACKAGE__->belongs_to('bookID' => 'MyApp::DBIC::Book'); |
ee38fa40 |
60 | |
dfeba824 |
61 | package MyApp::DBIC::Book; |
dfeba824 |
62 | use base qw/MyApp::DBIC/; |
ee38fa40 |
63 | |
dfeba824 |
64 | __PACKAGE__->table('books'); |
65 | __PACKAGE__->add_columns(qw/bookID title edition isbn publisher year/); |
66 | __PACKAGE__->set_primary_key(qw/bookID/); |
67 | |
68 | __PACKAGE__->has_many('b2a' => 'MyApp::DBIC::Book2Author', 'bookID'); |
ee38fa40 |
69 | |
dfeba824 |
70 | sub authors { |
71 | my ($self) = @_; |
72 | return MyApp::DBIC::Author->search( |
73 | { 'b2a.bookID' => $self->bookID }, # WHERE clause |
8ab99068 |
74 | { join => 'b2a' }); # join condition (part of search attrs) |
dfeba824 |
75 | } |
76 | |
77 | # So the above search returns an author record where the bookID field of the |
78 | # book2author table equals the bookID of the books (using the bookID |
79 | # relationship table |
ee38fa40 |
80 | |
81 | =item Advanced Exception handling |
82 | |
83 | =item Transactions |
84 | |
85 | =back |