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

Bill Moseley moseley at hank.org
Wed Mar 24 03:44:43 GMT 2010


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 =3D $cd->artist;  # hits the database again
   my $artist_id =3D $artist->id;

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 =3D $cd;
my @col =3D qw/ id title year artist /;

my %hash;
for my $accessor ( @col ) {

     $hash{$accessor} =3D $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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20100323/18c=
b0f8e/attachment.htm


More information about the DBIx-Class mailing list