An Introduction to Class::DBI - Table Relationships 28

Multi Table Relationships

Cleaning up accessor names

  • If you don't like how the names of your accessors are generated, you can override them

        package FilmBuff::Credit;
    
        sub accessor_name {
            my ($class, $column) = @_;
            $column =~ s/_id$//;
            return $column;
        }
    
  • However, for has_a and has_many you still refer to the original column name:

        FilmBuff::Credit->has_a('film_id'   => 'FilmBuff::Film');
        FilmBuff::Credit->has_a('person_id' => 'FilmBuff::Person');
    
  • Now you can use 'film' and 'person' instead of 'film_id' and 'person_id':

        package main;
    
        my ($actor) = FilmBuff::Person->search(name => 'Harrison Ford');
    
        print $actor->name . " has the following credits: \n";
    
        foreach my $credit ($actor->credits) {
            print $credit->film->year , "\t",
                  $credit->film->title , "\t",
                  $credit->type, "\n";
        }
    

 

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