An Introduction to Class::DBI - Table Relationships 24

Multi Table Relationships

Setting up Class::DBI

  • In Class::DBI we declare these table relationships in our Class:

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

 

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