[CDBI] Determining column relationship using __hasa_rels

Peter Speltz peterspeltz at gmail.com
Mon Oct 17 15:55:35 BST 2005


On 10/17/05, David Baird <lists.riverside at gmail.com> wrote:
> On 10/17/05, Kevin White <kevin at theconfused.co.uk> wrote:
> > Hi Folks,
> >
> > I am looking at Class::DBI::AsForm with a view to borrowing some bits
> > for a form builder suited to my own purposes and am having difficulty
> > with columns with relationships. The main problem is actually
> > identifying them. Is there anything I can use to check that a column
> > has a relationship on it? __hasa_rels is marked as deprecated in
> > Class::DBI and is returning undef.
> >
> > Thanks for any help
>
> Look at the meta_info() method instead.
>

Here is a method i put in my base CDBI class. It simply cycles thru
meta_info until it finds an accessor with its name.  It is from
Maypole::Model::CDBI with modification of mine.


###########
# related_class  #
###########

# Returns the class related to the accessor by a CDBI relationship.

sub related_class {
    my ( $self, $accessor ) = @_;
    my $meta = $self->meta_info;
    my @rels = keys %$meta;
    my $related;
    foreach (@rels) {
        $related = $meta->{$_}{$accessor};
        last if $related;
    }
    return unless $related;

    my $mapping = $related->{args}->{mapping};
    if ( $mapping and @$mapping ) {
        return $related->{foreign_class}->meta_info('has_a')->{
$$mapping[0] }->{foreign_class};
    }
    else {
        return $related->{foreign_class};
    }
}



pjs




More information about the ClassDBI mailing list