3 DBIx::Class::Manual::Cookbook - Misc receipes
7 =item Input validation.
11 =item Many-to-many relationships
13 This is not as easy as it could be, but it's possible. Here's an example to
16 # Set up inherited connection information
18 use base qw/DBIx::Class/;
20 __PACKAGE__->load_components(qw/PK::Auto::SQLite Core DB/);
21 __PACKAGE__->connection(...);
23 # Set up a class for the 'authors' table
24 package MyApp::DBIC::Author;
25 use base qw/MyApp::DBIC/;
27 __PACKAGE__->table('authors');
28 __PACKAGE__->add_columns(qw/authID first_name last_name/);
29 __PACKAGE__->set_primary_key(qw/authID/);
31 # Define relationship to the link table
32 __PACKAGE__->has_many('b2a' => 'MyApp::DBIC::Book2Author', 'authID');
34 # Create the accessor for books from the ::Author class
37 return MyApp::DBIC::Book->search(
38 { 'b2a.authID' => $self->authID }, # WHERE clause
39 { join => 'b2a' } # join condition (part of search attrs)
40 # 'b2a' refers to the relationship named earlier in the Author class.
41 # 'b2a.authID' refers to the authID column of the b2a relationship,
42 # which becomes accessible in the search by being joined.
46 # define the link table class
47 package MyApp::DBIC::Book2Author;
48 use base qw/MyApp::DBIC/;
50 __PACKAGE__->table('book2author');
51 __PACKAGE__->add_columns(qw/bookID authID/);
52 __PACKAGE__->set_primary_key(qw/bookID authID/);
54 __PACKAGE__->belongs_to('authID' => 'MyApp::DBIC::Author');
55 __PACKAGE__->belongs_to('bookID' => 'MyApp::DBIC::Book');
57 package MyApp::DBIC::Book;
58 use base qw/MyApp::DBIC/;
60 __PACKAGE__->table('books');
61 __PACKAGE__->add_columns(qw/bookID title edition isbn publisher year/);
62 __PACKAGE__->set_primary_key(qw/bookID/);
64 __PACKAGE__->has_many('b2a' => 'MyApp::DBIC::Book2Author', 'bookID');
68 return MyApp::DBIC::Author->search(
69 { 'b2a.bookID' => $self->bookID }, # WHERE clause
70 { join => 'b2a' }); # join condition (part of search attrs)
73 # So the above search returns an author record where the bookID field of the
74 # book2author table equals the bookID of the books (using the bookID
77 =item Advanced Exception handling