From c5fef33090759dc36f098995897d35881d8437df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joonas=20Kylm=C3=A4l=C3=A4?= Date: Fri, 14 Aug 2020 07:14:15 +0000 Subject: [PATCH] Bug 20447: (follow-up) Add required metadata field when creating holdings Since metadata column is mandatory in the holdings_metadata table we need to define it already when it is first created instead of creating it first and updating the column immediately after. This fixes the following error during holdings record creation: DBIx::Class::Storage::DBI::_dbh_execute(): Field 'metadata' doesn't have a default value at /kohadevbox/koha/Koha/Objects.pm line 114 Signed-off-by: Michal Denar Signed-off-by: Michal Denar --- Koha/Holding.pm | 19 +++++++------------ Koha/Objects.pm | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/Koha/Holding.pm b/Koha/Holding.pm index 5893ccf468..42b4a0bdd7 100644 --- a/Koha/Holding.pm +++ b/Koha/Holding.pm @@ -175,22 +175,17 @@ sub store { holding_id => $self->holding_id(), format => 'marcxml', schema => $marcflavour, + metadata => $marc_record->as_xml_record($marcflavour), }; - my $metadata_record = Koha::Holdings::Metadatas->find_or_create($metadata); - $metadata_record->metadata($marc_record->as_xml_record($marcflavour)); - - $result = $metadata_record->store() ? $self : undef; - - if ($result) { - $guard->commit() if defined $guard; + Koha::Holdings::Metadatas->update_or_create($metadata); + $guard->commit() if defined $guard; - # request that bib be reindexed so that any holdings-derived fields are updated - C4::Biblio::ModZebra( $self->biblionumber(), 'specialUpdate', 'biblioserver' ); + # request that bib be reindexed so that any holdings-derived fields are updated + C4::Biblio::ModZebra( $self->biblionumber(), 'specialUpdate', 'biblioserver' ); - logaction('CATALOGUING', $action, $self->holding_id(), 'holding') if C4::Context->preference('CataloguingLog'); - } + logaction('CATALOGUING', $action, $self->holding_id(), 'holding') if C4::Context->preference('CataloguingLog'); - return $result; + return $self; } =head3 delete diff --git a/Koha/Objects.pm b/Koha/Objects.pm index 3ea4727048..c55013c670 100644 --- a/Koha/Objects.pm +++ b/Koha/Objects.pm @@ -102,6 +102,24 @@ sub find { return $object; } +=head3 Koha::Objects->update_or_create(); + +my $object = Koha::Objects->update_or_create( $attrs ); + +=cut + +sub update_or_create { + my ( $self, $params ) = @_; + + my $result = $self->_resultset->update_or_create($params); + + return unless $result; + + my $object = $self->object_class->_new_from_dbic($result); + + return $object; +} + =head3 Koha::Objects->find_or_create(); my $object = Koha::Objects->find_or_create( $attrs ); -- 2.25.1