[CDBI] has_many - how to specify link columns ?

Andreas Grau agrau at esquat.com
Tue Aug 15 10:22:38 BST 2006


I just can not figure out how to specify the column names when using
has_many. I hope some kind soul can help me out.

Say I have two tables:

articles
  id varchar(32) primary key
  title varchar(255)
  baseprice double

price2articles
  id varchar(32) primary key
  artid varchar(32)
  price double

I need to link between articles.id and price2articles.artid - CDBI seems to insist on using the same column names, 'id' in this case.


So in package 'price2articles' I have
__PACKAGE__->has_a( 'artid' => ['articles']); # this direction works

But what to say in 'articles' ? I just can't seem to find to describe
the relationship, and google fails on me as well. All tutorials happen
to share the same id names and primary keys, which is not the case here.


__PACKAGE__->has_many( 'prices' => ['price2articles' => 'artid'], 'id');


And my driver:

my $artlist = new articles->retrieve_all();

while (my $article = $artlist->next) {
    
    print $article->title . " " . $article->baseprice; # works
    
    ## nothing here :-(
    my @prices = $article->prices;
    foreach(@prices) {
        print " " . $_->price ;
    }
    print "\n";

}

This lists the articles, but nothing for the prices.

Just for the records, I have the packages in different files, so the
ordering problem described elsewhere should not be reason. The has_a
works as well, I can 'go up' the relationship.

I am pretty confident that this is simple as nothing, but I miserably
fail to detect the flaw.

Anybody ?

Thanks a ton in advance,
Andreas



More information about the ClassDBI mailing list