[Dbix-class] How to elegantly memoize method calls to related tables
Robert Rothenberg
robrwo at gmail.com
Wed May 2 17:34:01 GMT 2012
Suppose we have two tables: events and people, where each event has a person
associated with it, e.g. in MySchema::Result::Event,
__PACKAGE__->belongs_to(
"person",
"MySchema::Result::Person",
{ id => "person_id" },
{ is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" },
);
The naive way of handling events is to do something like
foreach my $event (@events) {
my $person = $event->person;
...
}
but that is inefficient when there are many events associated with a small
number of people.
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?
More information about the DBIx-Class
mailing list