From 6235d0cfa16e5278a32ba6a4814f67ee4ad575b1 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 28 Apr 2020 17:14:41 +0200 Subject: [PATCH] Bug 23185: Make Koha::Objects->update loop on the object set if needed Signed-off-by: Bernardo Gonzalez Kriegel Signed-off-by: Martin Renvoize Signed-off-by: Victor Grousset/tuxayo Signed-off-by: Tomas Cohen Arazi Signed-off-by: Martin Renvoize --- Koha/Exceptions/Object.pm | 4 ++++ Koha/Object.pm | 3 ++- Koha/Objects.pm | 32 ++++++++++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Koha/Exceptions/Object.pm b/Koha/Exceptions/Object.pm index 488e5764ff..53bd44ca90 100644 --- a/Koha/Exceptions/Object.pm +++ b/Koha/Exceptions/Object.pm @@ -55,6 +55,10 @@ use Exception::Class ( description => 'Invalid data passed', fields => ['type', 'property', 'value'], }, + 'Koha::Exceptions::Object::NotInStorage' => { + isa => 'Koha::Exceptions::Object', + description => 'The object is not in storage yet', + }, ); sub full_message { diff --git a/Koha/Object.pm b/Koha/Object.pm index 44ef617824..ed7cc4bdc9 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -208,7 +208,8 @@ A shortcut for set + store in one call. sub update { my ($self, $values) = @_; - return $self->set($values)->store(); + Koha::Exceptions::Object::NotInStorage->throw unless $self->in_storage; + $self->set($values)->store(); } =head3 $object->delete(); diff --git a/Koha/Objects.pm b/Koha/Objects.pm index eb77c0e0e6..7512f2616f 100644 --- a/Koha/Objects.pm +++ b/Koha/Objects.pm @@ -194,6 +194,34 @@ sub delete { return $self->_resultset->delete; } +=head3 update + +=cut + +sub update { + my ($self, $params) = @_; + + my $no_triggers = delete $params->{no_triggers}; + + if ( + !$no_triggers + && ( Class::Inspector->function_exists( $self->object_class, 'update' ) + or Class::Inspector->function_exists( $self->object_class, 'store' ) ) + ) + { + my $objects_updated; + $self->_resultset->result_source->schema->txn_do( sub { + while ( my $o = $self->next ) { + $o->update($params); + $objects_updated++; + } + }); + return $objects_updated; + } + + return $self->_resultset->update($params); +} + =head3 single my $object = Koha::Objects->search({}, { rows => 1 })->single @@ -474,14 +502,14 @@ The autoload method is used call DBIx::Class method on a resultset. Important: If you plan to use one of the DBIx::Class methods you must provide relevant tests in t/db_dependent/Koha/Objects.t -Currently count, is_paged, pager, update, result_class, single and slice are covered. +Currently count, is_paged, pager, result_class, single and slice are covered. =cut sub AUTOLOAD { my ( $self, @params ) = @_; - my @known_methods = qw( count is_paged pager update result_class single slice ); + my @known_methods = qw( count is_paged pager result_class single slice ); my $method = our $AUTOLOAD; $method =~ s/.*:://; -- 2.39.5