An Introduction to Class::DBI - Table Relationships 31

Multi Table Relationships

Many to Many

  • We can install a method in the Person class to do this for us:

        package FilmBuff::Person;
    
        FilmBuff::Person->has_many('films_via_credits', FilmBuff::Credit => 'person_id');
        sub films {
            my $self = shift;
            return map $_->film, $self->films_via_credits;
        }
    
        package main;
    
        print $actor->name . " acted in the following films: \n";
        foreach my $film ($actor->films) {
            print $film->year , "\t", $film->title , "\n";
        }
    
  • Or we can even have Class::DBI do it for us:

        package FilmBuff::Person;
        FilmBuff::Person->has_many('films', [ FilmBuff::Credit => 'film' ], 'person_id');
    
        package main;
    
        print $actor->name . " acted in the following films: \n";
        foreach my $film ($actor->films) {
            print $film->year , "\t", $film->title , "\n";
        }
    

 

YAPC::Canada << Previous | Index | Next >>
Copyright © 2003 Michael Graham