From 54ef400f3d3db4a715195a0bed78879875ec1d2e Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 28 Oct 2024 11:49:13 +0100 Subject: [PATCH] Bug 38273: Make Koha::Object->discard_changes chainable Signed-off-by: Tomas Cohen Arazi --- Koha/Object.pm | 14 +++++++++++++- t/db_dependent/Koha/Object.t | 13 ++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Koha/Object.pm b/Koha/Object.pm index 8139acfaa98..6b6faad84d8 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -218,6 +218,18 @@ sub store { } } +=head3 discard_changes + +Refetch the row from the DB + +=cut + +sub discard_changes { + my ($self) = @_; + my $object_class = Koha::Object::_get_object_class( $self->_result->result_class ); + return $object_class->_new_from_dbic( $self->_result->discard_changes ); +} + =head3 $object->update(); A shortcut for set + store in one call. @@ -1020,7 +1032,7 @@ sub AUTOLOAD { return $accessor->( $self, @_ ); } - my @known_methods = qw( is_changed id in_storage get_column discard_changes make_column_dirty ); + my @known_methods = qw( is_changed id in_storage get_column make_column_dirty ); Koha::Exceptions::Object::MethodNotCoveredByTests->throw( error => sprintf("The method %s->%s is not covered by tests!", ref($self), $method), diff --git a/t/db_dependent/Koha/Object.t b/t/db_dependent/Koha/Object.t index 9f751c80b3b..0cb41f19320 100755 --- a/t/db_dependent/Koha/Object.t +++ b/t/db_dependent/Koha/Object.t @@ -153,7 +153,7 @@ subtest 'get_column' => sub { }; subtest 'discard_changes' => sub { - plan tests => 1; + plan tests => 3; $schema->storage->txn_begin; @@ -166,6 +166,17 @@ subtest 'discard_changes' => sub { dt_from_string->truncate( to => 'day' ), 'discard_changes should refresh the object' ); + my $cardnumber = $patron->cardnumber; + my $categorycode = $patron->categorycode; + my $branchcode = $patron->branchcode; + $patron->delete; + + $patron = + Koha::Patron->new( { cardnumber => $cardnumber, categorycode => $categorycode, branchcode => $branchcode } ) + ->store->discard_changes; + + is( ref($patron), 'Koha::Patron', 'discard_changes should return a Koha::Object object' ); + isnt( $patron->updated_on, undef, 'discard_changes should have fetched the row from the DB' ); $schema->storage->txn_rollback; }; -- 2.47.0