From 75456ba973f87d3c46d045811ad32dfe2a153e3f 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 --- 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 1d9604ca39..6db7caf9fd 100644 --- a/Koha/Hold.pm +++ b/Koha/Hold.pm @@ -347,9 +347,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 8db8cecd36..491e200f19 100644 --- a/Koha/Schema/Result/Reserve.pm +++ b/Koha/Schema/Result/Reserve.pm @@ -408,6 +408,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