[Dbix-class] Detecting a "belongs_to" relationship.

Rolf Schaufelberger rs at plusw.de
Wed Mar 24 07:55:22 GMT 2010


Am 24.03.2010 um 04:44 schrieb Bill Moseley:

> 
> In my music database I have CD objects that belong to an Artist.  I want the artist primary key but I don't want to hit
> the database (or do a join with a prefetch) just to get the id.
> 
>    my $artist = $cd->artist;  # hits the database again
>    my $artist_id = $artist->id;
> 

I define my relationships like that :

belongs_to ( artist => 'My::Schema::Artist','artist_id' );

so   
$cd->artist gives me the artist object
and 
$cd->artist_id  gives me just the key.




> I know I can use get_column() to get the column data, but I still want to inflate other columns.  So, normally I want to call the accessor on the object.
> 
> So, basically, what I'm looking to do is a way to detect when to use get_column() in a generic way:
> 
> my $item = $cd;
> my @col = qw/ id title year artist /;
> 
> my %hash;
> for my $accessor ( @col ) {
> 
>      $hash{$accessor} = $item->is_belongs_to_relationship( $accessor )
>          ? $item->get_column( $accessor )  # return raw foreign key value
>          : $item->$accessor;
> }
> 
> 
> What's an efficient way to detect the belongs_to relationship?
> 
> 
> 
> Thanks,
> 
> 
> -- 
> Bill Moseley
> moseley at hank.org
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk

Mit freundlichen Grüßen
Rolf Schaufelberger
Geschäftsführer 

plusW GmbH
Vorstadtstr. 61  -67   Tel.   07181 47 47 305
73614 Schorndorf     Fax.  07181 47 45 344

www.plusw.de
www.mypixler.com
www.calendrino.de









More information about the DBIx-Class mailing list