An Introduction to Class::DBI - Table Relationships 26

Multi Table Relationships

A Digression on Style

  • If you find __PACKAGE__ ugly, then you can pretty it up with 'CLASS'

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

 

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