X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FManual%2FCookbook.pod;h=57c21756a29a3dd0637d9ec474bd8c90043dfc81;hb=130c64391b48bae9eb374e931c7d6c308625bf6b;hp=ee5ea2203bc7a8584fe03fce71289d0493253ecf;hpb=3b44ccc6d4d74f6016ad8f5eb781fd46e37875e6;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Manual/Cookbook.pod b/lib/DBIx/Class/Manual/Cookbook.pod index ee5ea22..57c2175 100644 --- a/lib/DBIx/Class/Manual/Cookbook.pod +++ b/lib/DBIx/Class/Manual/Cookbook.pod @@ -1,70 +1,85 @@ =head1 NAME -DBIx::Class::Manual::Cookbook - Misc receipes +DBIx::Class::Manual::Cookbook - Misc recipes -=over 4 +=head1 DESCRIPTION -=item Input validation. +Things that could be handy -=item Using joins +=head1 RECIPES -=item Many-to-many relationships +=head2 Input validation. -This is not as easy as it could be, but it's possible. Here's an example to -illustrate: - - package Base; - - use base qw/DBIx::Class/; - - __PACKAGE__->load_components(qw/Core DB/); - __PACKAGE__->connection(...); - - package Left; - - use base qw/Base/; - - __PACKAGE__->table('left'); - __PACKAGE__->add_columns(qw/id left_stuff/); - __PACKAGE__->set_primary_key(qw/id/); - __PACKAGE__->has_many('mid' => 'Mid'); - - sub right { - my ($self) = @_; - return Right->search( - { 'left.id' => $self->id }, - { join => { 'mid' => 'left' }}); - } +=head2 Using joins - package Mid; +=head2 Many-to-many relationships - use base qw/Base/; - - __PACKAGE__->table('mid'); - __PACKAGE__->add_columns(qw/left right/); - __PACKAGE__->set_primary_key(qw/left right/); - - __PACKAGE__->belongs_to('left' => 'Left'); - __PACKAGE__->belongs_to('right' => 'Right'); - - package Right; - - use base qw/Base/; - - __PACKAGE__->table('right'); - __PACKAGE__->add_columns(qw/id right_stuff/); - __PACKAGE__->set_primary_key(qw/id/); - __PACKAGE__->has_many('mid' => 'Mid'); - - sub left { - my ($self) = @_; - return Left->search( - { 'right.id' => $self->id }, - { join => { 'mid' => 'right' }); - } - -=item Advanced Exception handling +This is not as easy as it could be, but it's possible. Here's an example to +illustrate: -=item Transactions + # Set up inherited connection information + package MyApp::DBIC; + use base qw/DBIx::Class/; + + __PACKAGE__->load_components(qw/PK::Auto::SQLite Core DB/); + __PACKAGE__->connection(...); + + # Set up a class for the 'authors' table + package MyApp::DBIC::Author; + use base qw/MyApp::DBIC/; + + __PACKAGE__->table('authors'); + __PACKAGE__->add_columns(qw/authID first_name last_name/); + __PACKAGE__->set_primary_key(qw/authID/); + + # Define relationship to the link table + __PACKAGE__->has_many('b2a' => 'MyApp::DBIC::Book2Author', 'authID'); + + # Create the accessor for books from the ::Author class + sub books { + my ($self) = @_; + return MyApp::DBIC::Book->search( + { 'b2a.authID' => $self->authID }, # WHERE clause + { join => 'b2a' } # join condition (part of search attrs) + # 'b2a' refers to the relationship named earlier in the Author class. + # 'b2a.authID' refers to the authID column of the b2a relationship, + # which becomes accessible in the search by being joined. + ); + } + + # define the link table class + package MyApp::DBIC::Book2Author; + use base qw/MyApp::DBIC/; + + __PACKAGE__->table('book2author'); + __PACKAGE__->add_columns(qw/bookID authID/); + __PACKAGE__->set_primary_key(qw/bookID authID/); + + __PACKAGE__->belongs_to('authID' => 'MyApp::DBIC::Author'); + __PACKAGE__->belongs_to('bookID' => 'MyApp::DBIC::Book'); + + package MyApp::DBIC::Book; + use base qw/MyApp::DBIC/; + + __PACKAGE__->table('books'); + __PACKAGE__->add_columns(qw/bookID title edition isbn publisher year/); + __PACKAGE__->set_primary_key(qw/bookID/); + + __PACKAGE__->has_many('b2a' => 'MyApp::DBIC::Book2Author', 'bookID'); + + sub authors { + my ($self) = @_; + return MyApp::DBIC::Author->search( + { 'b2a.bookID' => $self->bookID }, # WHERE clause + { join => 'b2a' }); # join condition (part of search attrs) + } + + # So the above search returns an author record where the bookID field of the + # book2author table equals the bookID of the books (using the bookID + # relationship table + +=head2 Advanced Exception handling + +=head2 Transactions =back