[Catalyst] CDBI Relationships

Eric Black black.eric at gmail.com
Thu Aug 18 17:09:13 CEST 2005


Hi,

Still having problems figuring out how to set up relationships between
tables. I see how the relationships are done in the Hops example, but
I was hoping I wouldn't need the Class::DBI::Loader::Relationship
class and could set up the relationship per table/class in my example
app so I could understand how Catalyst is working a little better.


I have a simple example Catalyst program I'm working with that has
three database tables:


mysql> describe author;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| author_id  | int(11)      |      | PRI | NULL    | auto_increment |
| first_name | varchar(128) |      |     |         |                |
| last_name  | varchar(128) |      |     |         |                |
+------------+--------------+------+-----+---------+----------------+

mysql> describe book;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| book_id      | int(11)      |      | PRI | NULL    | auto_increment |
| title        | varchar(255) |      |     |         |                |
| isbn         | varchar(24)  |      |     |         |                |
| publisher_id | int(11)      |      |     | 0       |                |
| author_id    | int(11)      |      |     | 0       |                |
+--------------+--------------+------+-----+---------+----------------+

mysql> describe publisher;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| publisher_id | int(11)      |      | PRI | NULL    | auto_increment |
| name         | varchar(128) |      |     |         |                |
+--------------+--------------+------+-----+---------+----------------+


So, I created the models like so:

script/bookdb_create.pl model CDBI CDBI dbi:mysql:bookstore

And I got the one main CDBI.pm file and the empty class files for
Author, Book, and Publisher that inherit. Now when I display the form
for the Book table, I want to display the publisher name instead of
the publisher_id and the author name(or at least last name) for
author_id. I tried adding this to the BookDB::M::CDBI::Book package:


__PACKAGE__->has_a( author_id    => 'BookDB::M::CDBI::Author' );
__PACKAGE__->has_a( publisher_id => 'BookDB::M::CDBI::Publisher' );
__PACKAGE__->columns( Stringify => qw/last_name/ );

This Stringify part is from the Class::DBI perldocs. I thought that
the first line would automatically tie the author_id to the Author
class, but I'm missing the part that will allow me to specify how to
replace the book table's author_id with the author table's last_name.

Any help would be appreciated.

Thanks,

Eric



More information about the Catalyst mailing list