Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Assume

package Role; Role->table('role'); Role->columns(Primary => qw/film actor/); Role->has_a(film => 'Film'); package Film; Film->table('film'); Film->columns(All => qw/id title rating/); Film->has_many(stars => [ Role => 'actor' ]);
I want a list of all the films in which a given actor stared. Note that Role has two primary keys.
Film->stars gives me all of the role objects, I would like to do something like:
$film = Film->search(title => 'Jaws'); $film->stars;
Or even better;
$stars = Film->search(title => 'Jaws')->stars;
Can I coax Class::DBI into this?

Replies are listed 'Best First'.
Re: Basic Class::DBI question
by perrin (Chancellor) on Feb 01, 2006 at 04:36 UTC
Re: Basic Class::DBI question
by davidrw (Prior) on Feb 01, 2006 at 04:37 UTC
    You can always write a method for Film to wrap all the steps ... (note: untested & no error handling is obviously absent)
    package File; Film->table('film'); Film->columns(All => qw/id title rating/); sub stars { my $self = shift; my $title = shift; my ($film) = $self->search( title => $title ); return [ map { $_->actor } Role->search(film => $film->id) ]; } package main; @$aref = Film->stars('Jaws');