From 9227b8c51c88d1f30ed7056ec10b8c8097a754a1 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 10 Nov 2020 17:37:21 -0300 Subject: [PATCH] Bug 26988: Make ->patron prefetchable Signed-off-by: Tomas Cohen Arazi Signed-off-by: Jonathan Druart (cherry picked from commit 75456ba973f87d3c46d045811ad32dfe2a153e3f) Signed-off-by: Andrew Fuerste-Henry --- Koha/Hold.pm | 5 ++-- Koha/Schema/Result/Reserve.pm | 7 +++++ t/db_dependent/Koha/Hold.t | 50 +++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100755 t/db_dependent/Koha/Hold.t diff --git a/Koha/Hold.pm b/Koha/Hold.pm index cbac5b82ae..63b135b424 100644 --- a/Koha/Hold.pm +++ b/Koha/Hold.pm @@ -291,9 +291,10 @@ Returns the related Koha::Patron object for this hold sub patron { my ($self) = @_; - $self->{_patron} ||= Koha::Patrons->find( $self->borrowernumber() ); + my $patron_rs = $self->_result->patron; + return unless $patron_rs; - return $self->{_patron}; + return Koha::Patron->_new_from_dbic($patron_rs); } =head3 item diff --git a/Koha/Schema/Result/Reserve.pm b/Koha/Schema/Result/Reserve.pm index c5460e34da..957107c9ef 100644 --- a/Koha/Schema/Result/Reserve.pm +++ b/Koha/Schema/Result/Reserve.pm @@ -364,6 +364,13 @@ __PACKAGE__->belongs_to( }, ); +__PACKAGE__->belongs_to( + "patron", + "Koha::Schema::Result::Borrower", + { "foreign.borrowernumber" => "self.borrowernumber" }, + { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" }, +); + __PACKAGE__->add_columns( '+item_level_hold' => { is_boolean => 1 }, '+lowestPriority' => { is_boolean => 1 }, diff --git a/t/db_dependent/Koha/Hold.t b/t/db_dependent/Koha/Hold.t new file mode 100755 index 0000000000..bdf4f499d2 --- /dev/null +++ b/t/db_dependent/Koha/Hold.t @@ -0,0 +1,50 @@ +#!/usr/bin/perl + +# Copyright 2020 Koha Development team +# +# This file is part of Koha +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 1; + +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +subtest 'patron() tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object({ class => 'Koha::Patrons' }); + my $hold = $builder->build_object( + { + class => 'Koha::Holds', + value => { + borrowernumber => $patron->borrowernumber + } + } + ); + + my $hold_patron = $hold->patron; + is( ref($hold_patron), 'Koha::Patron', 'Right type' ); + is( $hold_patron->id, $patron->id, 'Right object' ); + + $schema->storage->txn_rollback; +}; -- 2.39.5