aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/DB/Result/Problem.pm
diff options
context:
space:
mode:
authorMatthew Somerville <matthew-github@dracos.co.uk>2017-08-18 18:44:32 +0100
committerMatthew Somerville <matthew-github@dracos.co.uk>2017-08-18 19:22:48 +0100
commit20f93425b278ee2cd54b90cab6eb3e69c4921e73 (patch)
tree0b14068745a0e24435110423a68e491afd9e5b19 /perllib/FixMyStreet/DB/Result/Problem.pm
parent382326968ae1ee5a5226dfe0d52e13f02a292c0f (diff)
Cache Problem->bodies and prefetch the body areas.
Diffstat (limited to 'perllib/FixMyStreet/DB/Result/Problem.pm')
-rw-r--r--perllib/FixMyStreet/DB/Result/Problem.pm21
1 files changed, 14 insertions, 7 deletions
diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm
index 720e8cc93..19e76f6ed 100644
--- a/perllib/FixMyStreet/DB/Result/Problem.pm
+++ b/perllib/FixMyStreet/DB/Result/Problem.pm
@@ -502,13 +502,20 @@ Returns a hashref of bodies to which a report was sent.
=cut
-sub bodies($) {
- my $self = shift;
- return {} unless $self->bodies_str;
- my $bodies = $self->bodies_str_ids;
- my @bodies = $self->result_source->schema->resultset('Body')->search({ id => $bodies })->all;
- return { map { $_->id => $_ } @bodies };
-}
+has bodies => (
+ is => 'ro',
+ lazy => 1,
+ default => sub {
+ my $self = shift;
+ return {} unless $self->bodies_str;
+ my $bodies = $self->bodies_str_ids;
+ my @bodies = $self->result_source->schema->resultset('Body')->search(
+ { id => $bodies },
+ { prefetch => 'body_areas' },
+ )->all;
+ return { map { $_->id => $_ } @bodies };
+ },
+);
sub body_names($) {
my $self = shift;