8fd5e0ac6aafb6b5eb60b36b04783226167fb130
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Manual / Cookbook.pod
1 =head1 DBIx::Class Cookbook
2
3 =over 4
4
5 =item Input validation.
6
7 =item Using joins
8
9 =item Many-to-many relationships
10
11 This is not as easy as it could be, but it's possible. Here's an example to 
12 illustrate:
13
14     package Base; 
15
16     use base qw/DBIx::Class/;
17
18     __PACKAGE__->load_components(qw/Core DB/);
19     __PACKAGE__->connection(...);
20
21     package Left;
22
23     use base qw/Base/;
24
25     __PACKAGE__->table('left');
26     __PACKAGE__->add_columns(qw/id left_stuff/);
27     __PACKAGE__->set_primary_key(qw/id/);
28     __PACKAGE__->has_many('mid' => 'Mid');
29
30     sub right {
31      my ($self) = @_;
32      return Right->search(
33        { 'left.id' => $self->id },
34        { join => { 'mid' => 'left' }});
35     }
36
37     package Mid;
38
39     use base qw/Base/;
40
41     __PACKAGE__->table('mid');
42     __PACKAGE__->add_columns(qw/left right/);
43     __PACKAGE__->set_primary_key(qw/left right/);
44
45     __PACKAGE__->belongs_to('left' => 'Left');
46     __PACKAGE__->belongs_to('right' => 'Right');
47
48     package Right;
49
50     use base qw/Base/;
51
52     __PACKAGE__->table('right');
53     __PACKAGE__->add_columns(qw/id right_stuff/);
54     __PACKAGE__->set_primary_key(qw/id/);
55     __PACKAGE__->has_many('mid' => 'Mid');
56
57     sub left {
58      my ($self) = @_;
59      return Left->search(
60        { 'right.id' => $self->id },
61        { join => { 'mid' => 'right' });
62     }
63
64 =item Advanced Exception handling
65
66 =item Transactions
67
68 =back