An Introduction to Class::DBI - Table Relationships 25

Multi Table Relationships

A Digression on Style

  • Instead of naming your class all the time, you can use the __PACKAGE__ keyword

        package FilmBuff::DBI;
        use base 'Class::DBI';
        __PACKAGE__->set_db('Main', 'dbi:mysql:database=test', 'test', '');
        # FilmBuff::Person
        package FilmBuff::Person;
        use base 'FilmBuff::DBI';
        __PACKAGE__->columns('id', 'name');
        __PACKAGE__->columns(Primary => 'id');
        FilmBuff::Person->has_many('credits', FilmBuff::Credit => 'person_id');
        # FilmBuff::Film
        package FilmBuff::Film;
        use base 'FilmBuff::DBI';
        __PACKAGE__->columns('id', 'title', 'year');
        __PACKAGE__->columns(Primary => 'id');
        __PACKAGE__->has_many('credits', FilmBuff::Credit => 'film_id');
        # FilmBuff::Credit
        package FilmBuff::Credit;
        use base 'FilmBuff::DBI';
        __PACKAGE__->columns('id', 'type', 'person_id', 'film_id');
        __PACKAGE__->columns(Primary => 'id');
        __PACKAGE__->has_a('film_id'   => 'FilmBuff::Film');
        __PACKAGE__->has_a('person_id' => 'FilmBuff::Person');


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