Retrieving data from a has_many relation in DBIx :: Class

For the simple case of two tables - the term and the definition - where is the Term has_manyDefinitions and the definition belongs_toThe term, all members and corresponding definitions must be extracted and displayed in some way.

Here is what I came 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;
  }
  ...
}

He does the job, but I was wondering if a different, less violent approach could be made.

+3
source share
2 answers
my $terms= $schema->resultset('Term')->search(undef, {  
  prefetch => 'definitions',  
});  

my @terms = $terms->all;

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

, ; . , , , , . , , , , DBIx:: Class:: ResultSet.

+1

Source: https://habr.com/ru/post/1756463/


All Articles