[Dbix-class] Result Sets of Joins

William Reardon wdr1 at pobox.com
Mon Sep 29 16:32:03 BST 2008


Excuse me if this is an obvious question, but I'm still learning the
DBIx::Class way & haven't had any luck finding answers...

Coming from the common DBI/hand-crafted sql route, I'm used to the world
where I could join a many-to-one table and easily access columns from
either table in the result.  E.g.:  SELECT author.name, book.title
FROM..., getting a hashref of the result, them doing something like:

print $row->{name}, " by ", $row->{title}, "\n"

etc.  Is there anyway to do this w/ DBI?

I have my relationships stated in the Schema files, so something like
this *does* work for me:

my $rs = $schema->resultset('Authors')->search(
   {user_id => 1 },
   {prefetch => 'books',
    '+select' => [ 'books.purchase_price',
                   'books.title',
                 ],

    '+as' => [ 'purchase_price',
               'title',
             ],
    });

my @results = $rs->all;
foreach my $row (@results) {
   my $rs2 = $row->books;
   while (my $rel_row = $rs2->next) {
     print $rel_row->purchase_price, "\n";
   }
}


but it's not really what I want.  I could manually transform it, but
that seems like coding around my own ignorance.  Is there a way I could
get to close to something like:

foreach my $row (@results) {
   print $row->{name}, " by ", $row->{title}, "\n";
}


Thanks in advance.

-Bill




More information about the DBIx-Class mailing list