[Dbix-class] Re: How to elegantly memoize method calls to related tables

Adam Sjøgren asjo at koldfront.dk
Wed May 2 17:52:58 GMT 2012


On Wed, 02 May 2012 18:34:01 +0100, Robert wrote:

> What is the best way to modify customize the person method to memoize
> people, so that I can code the naive way (which is easier to read) but have
> DBIC do the right thing?

A way is to use prefetch, when you are getting the events; i.e.
something along the lines of:

  my $events=$schema->resultset('Result::Event')->search({}, { prefetch=>'person' });
  while (my $event=$events->next) {
      my $person=$event->person;
      # ...
  }

This tells DBIx::Class to join the person table on the event table and
person objects are then generated for you from that single query.

Running your script with DBIC_TRACE=1 set in the environment is an easy
way to check that prefetching indeed saves you roundtrips to the
database.


  Best regards,

    Adam

-- 
 "You have to photosynthesize"                                Adam Sjøgren
                                                         asjo at koldfront.dk



More information about the DBIx-Class mailing list