views:

46

answers:

2

Given a simple case of two tables - Term and Definition - where Term has_many Definitions and Definition belongs_to Term, all terms and the corresponding definitions are to be fetched and displayed somehow.

Here is what I've come up with so far:

my $terms= $schema->resultset('Term')->search(undef, {  
  prefetch => 'definitions',  
});  

while (my $term = $terms->next) {
  my @terms;
  push @terms, $term->term;

  my $definitions = $term->definitions;
  my @definitions;
  while (my $definition = $definitions->next) {
    push @definitions, $definitions;
  }
  ...
}

It does the job but I was wondering if a different, less crufty approach could be taken.

+1  A: 

DBIx::Class::Manual::Joining should help. See, for example, Whole related objects.

Sinan Ünür
@Sinan Ünür, The examples don't really show how to extract related data; in particular, when relationships are of type `multi`.
Stan
+1  A: 
my $terms= $schema->resultset('Term')->search(undef, {  
  prefetch => 'definitions',  
});  

my @terms = $terms->all;

my @definitions = map $_->definitions->all, @terms;

This looks like what you are trying to do; I can't really tell. The fact that you make a new array, push onto it, and then let it go out of scope doesn't really make any sense at all. Anyway, if I understand you correctly all you wanted was the all method from DBIx::Class::ResultSet.

Frew
@Frew, The array is nothing but a part of this example.The `all` method is what I wanted indeed. Thanks.
Stan