From 637e82729b76e406517c140f69e9071cc48ca199 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 16 Jan 2020 13:32:25 -0300 Subject: [PATCH] Bug 24435: Add Koha::Biblio->items_count This patch introduces a handy shortcut to get the items count for a biblio. So instead of calling: $count = $biblio->items->count; you can call: $count = $biblio->items_count; The use case for this, is when we want to embed information on objects to be sent as API responses. In that case, calling ->items->count needs to be done manually on the controller script, with interesting added complexity. With this kind of method, we can just require it to be embedded automagically by calling the following on the API: $biblio->to_api({ embed => { items_count => {} } }); If there are several nested layers of objects, doing manually can get really tricky and error prone. To test: 1. Apply this patches 2. Run: $ kshell k$ prove t/db_dependent/Koha/Biblio.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: David Nind Signed-off-by: Nick Clemens Signed-off-by: Martin Renvoize --- Koha/Biblio.pm | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index d0c291a528..7c6e47328c 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -411,6 +411,20 @@ sub items { return Koha::Items->_new_from_dbic( $items_rs ); } +=head3 items_count + +my $items_count = $biblio->items(); + +Returns the count of the the related Koha::Items object for this biblio + +=cut + +sub items_count { + my ($self) = @_; + + return $self->_result->items->count; +} + =head3 itemtype my $itemtype = $biblio->itemtype(); -- 2.39.5