in reply to Joins with Class::DBI

Check out the section on the has_many and has_a methods. You will want to add something like the following to your classes:

# To your authors class __PACKAGE__->has_many('books', 'My::DB::Books' => book_id); # To your books class __PACKAGE__->has_a('author_id', 'My::DB::Authors');

Then in your code you can use:

my $author = My::DB::Authors->retrieve($author_id); # method add_to_books is created for you by has_many $author->add_to_books({ col1 => value1, col2 => value2, ... }); # author_id will be filled in automatically my $books = $author->books; while (my $book = $books->next) { ... }

- Cees

Replies are listed 'Best First'.
Re: Re: Joins with Class::DBI
by thpfft (Chaplain) on Jul 19, 2003 at 23:14 UTC

    Not quite. This would be correct if there were a one-to-many relationship between authors and books (ie if each book was allowed only one author), but the OP has a many-to-many relationship there - presumably on the grounds that some books have several authors - and, like everyone, is wondering how to make the joining table invisible. The short answer is that s/he does have to create a My::DB::AuthorBook joining class, but s/he will probably never need to access it directly. It's in the pod under 'mapping', a subheading of 'relationships'.