Bugzilla – Attachment 183660 Details for
Bug 40275
Add Koha::Patrons->find_by_identifier()
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 40275: Introduce `Koha::Patrons->find_by_identifier()`
Bug-40275-Introduce-KohaPatrons-findbyidentifier.patch (text/plain), 2.46 KB, created by
David Nind
on 2025-06-30 21:41:59 UTC
(
hide
)
Description:
Bug 40275: Introduce `Koha::Patrons->find_by_identifier()`
Filename:
MIME Type:
Creator:
David Nind
Created:
2025-06-30 21:41:59 UTC
Size:
2.46 KB
patch
obsolete
>From 9c6ee80b5bcdb068261c5287707c01e60034f232 Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Mon, 30 Jun 2025 10:51:14 -0300 >Subject: [PATCH] Bug 40275: Introduce `Koha::Patrons->find_by_identifier()` > >In our codebase, there's a pattern we tend to use for searching patrons >by userid and falling back to find them by cardnumber: > >* C4::Auth >* C4::SIP::ILS::Patron >* (slightly different) Koha::REST::V1::Auth::Password >* (slightly different) Koha::REST::V1::Auth >* C4::Circulation >* C4::Auth_with_cas > >It generally uses the following pattern: > >```perl >my $patron = Koha::Patrons->find( { userid => $identifier } ); >$patron //= Koha::Patrons->find( { cardnumber => $identifier } ); >``` > >The problem with this is that `find` actually produces a DBIX::Class >warn because `find` is being called without primary key parameters. I >haven't seen it in the wild, until the latest change in >`checkpw_internal` which made it throw warnings in SIP. My >interpretation is that SIP's special approach with the Trapper.pm class >made it show where other places just get it hidden. > >That said, I implemented this using `search()` to overcome this. > >To test: >1. Apply the patches >2. Run: > $ ktd --shell > k$ prove t/db_dependent/Koha/Patrons.t >=> SUCCESS: Tests pass! >3. Sign off :-D > >Signed-off-by: David Nind <david@davidnind.com> >--- > Koha/Patrons.pm | 27 ++++++++++++++++++++++++++- > 1 file changed, 26 insertions(+), 1 deletion(-) > >diff --git a/Koha/Patrons.pm b/Koha/Patrons.pm >index 3156f4f60e..a1e83328ff 100644 >--- a/Koha/Patrons.pm >+++ b/Koha/Patrons.pm >@@ -37,10 +37,35 @@ Koha::Patron - Koha Patron Object class > > =head1 API > >-=head2 Class Methods >+=head2 Class methods > > =cut > >+=head3 find_by_identifier >+ >+ my $patron = Koha::Patrons->find_by_identifier($identifier); >+ >+Find a patron by either userid or cardnumber. Returns the first patron found >+or undef if no patron matches the identifier. >+ >+This method searches first by userid, then by cardnumber if no match is found. >+ >+=cut >+ >+sub find_by_identifier { >+ my ( $self, $identifier ) = @_; >+ >+ return unless defined $identifier && $identifier ne ''; >+ >+ # First try to find by userid >+ my $patron = $self->search( { userid => $identifier } )->next; >+ >+ # If not found by userid, try cardnumber >+ $patron = $self->search( { cardnumber => $identifier } )->next unless $patron; >+ >+ return $patron; >+} >+ > =head3 search_limited > > my $patrons = Koha::Patrons->search_limit( $params, $attributes ); >-- >2.39.5
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 40275
:
183647
|
183648
|
183649
|
183659
| 183660 |
183661