Discover Class::DBI relationships
By Diona Kidd on Mar 11, 2005 in Technical
I discovered an undocumented method in Class::DBI today that allows discovery of relationships (has_a, has_many). The only meaningful argument for this method is the relationship type name (’has_a’ or ‘has_many’).
This example shows how to retrieve the method names of has_many relationships. The resulting hash in the following example contains the relationship method name as the key and ‘Class::DBI::Relationship::HasMany’ as the key value.
my $self; # instantiated Class::DBI object
my $meta = $self->meta_info('has_many');
if($meta && (ref($meta) eq 'HASH') ) {
# found a has_many relationship
foreach(keys %$meta) {
warn "meta: $_ ".$meta->{$_}n";
}
}
You can expand on this method by adding additional information to the argument for the ‘meta’ method (e.g. - add the name of the relationship method as a value to the ‘relationship type’ key). This returns information one level deeper.
my $self; # instantiated Class::DBI object
my $meta = $self->meta_info('has_many' => 'emails');
if($meta && (ref($meta) eq 'HASH') ) {
# found a has_many relationship
foreach(keys %$meta) {
warn "meta: $_ ".$meta->{$_}n";
}
References: Class::DBI Wiki

Sorry, comments for this entry are closed at this time.