Re: Foreign key as primary key...
[prev]
[thread]
[next]
[Date index for 2005/02/20]
Perhaps it would avoid confusion and emails if the Class::DBI docs
explained that has_a and has_many are for defining both sides of a
one-to-many relationship, and might_have is for defining a one-to-one
relationship? I know this is obvious to any intelligent person who has
had to go through the implementation of both at least once, but at first
glance, CDBI is concept-rich and thefore overwhelming.
Sticking a version of this at the top of the "TABLE RELATIONSHIPS"
section as part of the introduction would probably make the whole thing
go down quicker and smoother, like a spoonful of sugar:
---
Class::DBI directly supports one-to-one and one-to-many relationships,
and indirectly supports many-to-many relationship. To define a
one-to-one relationship, use the 'might_have' method from the dominant
table (the one you would typically put on the left side of a LEFT
JOIN). To define a one-to-many relationship, use the 'has_a' and
'has_many' methods, each from the appropriate table. If you have an
'account' table that has a one-to-many relationship with a
'monthly_statement' table, you would do something like this:
package Account;
__PACKAGE__->has_many( monthly_statements => 'MonthlyStatement' ); #
adds monthly_statements method
package MonthlyStatement;
__PACKAGE__->has_a( account_id => 'Account' ); # 'inflates' account_id
value into an Account object
---
I'm still new, so I'll let someone else right the many-to-many intro
paragraph. :)
-ofer
|
(message missing)
|