[CDBI] has_many - how to specify link columns ?
Andreas Grau
agrau at esquat.com
Tue Aug 15 22:19:40 BST 2006
Top. This actually does exactly what I was after. Thanks a ton,
Michael, Ed and all for your input.
Best from France,
Andreas
On Tue, 15 Aug 2006 09:53:42 -0700
Michael Reece <mreece at vinq.com> wrote:
> i think what you want is simply:
>
> articles->has_many( prices => 'prive2articles' => 'artid' ); #
> third argument is 'my key in foreign table'
>
>
> this syntax ['price2articles' => 'artid'] is for 'mapping' across a
> many-to-many table.
>
> (for example, if price2articles.price were (instead of a float) a key
> to another table (price), then you might:
>
> __PACKAGE__->has_many( prices => [ 'prices2articles' => 'price' ]
> => 'artid' );
>
> and that would automatically map the "price2articles->has_a
> (price=>'prices')" relationship, as though you had done this instead:
>
> __PACKAGE__->has_many( _article_prices => 'prices2articles' =>
> 'artid' );
> sub prices {
> my $self = shift;
> return map $_->price, $self-> _article_prices;
> }
> )
>
> michael.
>
> On Aug 15, 2006, at 2:22 AM, Andreas Grau wrote:
>
> >
> > 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
> >
> > _______________________________________________
> > ClassDBI mailing list
> > ClassDBI at lists.digitalcraftsmen.net
> > http://lists.digitalcraftsmen.net/mailman/listinfo/classdbi
>
> ---
> michael reece :: software engineer :: mreece at vinq.com
>
More information about the ClassDBI
mailing list