[Catalyst] CDBI Relationships
Scottsweep
scottsweep at yahoo.com
Thu Aug 18 20:08:06 CEST 2005
For Class::DBI::Loader to work with relationships
you'd need actual foreign keys set up in your
database. Loader works by reading these FK's to
determine the has_a/has_many it needs to setup between
objects. No worries though, you're on the right path
to build your own.
> 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/ );
Just set up the other side of the relationship in your
other modules:
in BookDB::M::CDBI::Author
__PACKAGE__->has_many( books =>
'BookDB::M::CDBI::Book' );
# your Stringify belongs in BookDB::M::CDBI::Author,
# not in BookDB::M::CDBI::Book, Book has no last_name
__PACKAGE__->columns( Stringify => qw/last_name/ );
in BookDB::M::CDBI::Publisher
__PACKAGE__->has_many( books =>
'BookDB::M::CDBI::Book' );
__PACKAGE__->columns( Stringify => qw/last_name/ );
where 'books' is really any method you want as in
http://search.cpan.org/~tmtm/Class-DBI-0.96/lib/Class/DBI.pm#has_many
then in your template something like:
[% book.author_id.last_name %]
OR the following should produce last_name with the
Stringify option above
[% book.author_id %]
as now $book->author_id will return an instance of
BookDB::M::CDBI::Author
Hope this helps,
Scott Connelly
____________________________________________________
Start your day with Yahoo! - make it your home page
http://www.yahoo.com/r/hs
More information about the Catalyst
mailing list