From 550a0f8213d521044a5d688a7d0a5f038b55aa39 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Thu, 19 Apr 2018 09:27:35 -0400 Subject: [PATCH] Bug 20621: Add ability for Koha::Object objects to update themselves with database generated value automatically Koha objects should, on store() update themselves from the db if the database creates any of the columns. That way we don't have to call discard_changes() manually. See https://wiki.koha-community.org/wiki/Koha_Objects#store_doesn.27t_return_the_DB_object for details. --- Koha/Object.pm | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Koha/Object.pm b/Koha/Object.pm index 1f54273493..dbb645b2ad 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -121,7 +121,9 @@ sub store { my ($self) = @_; try { - return $self->_result()->update_or_insert() ? $self : undef; + my $object = $self->_result()->update_or_insert() ? $self : undef; + $object->discard_changes if $object->_has_derived_columns && defined wantarray; + return $object; } catch { # Catch problems and raise relevant exceptions @@ -401,6 +403,17 @@ sub AUTOLOAD { return $r; } +=head3 _has_derived_columns + +This method is a flag that tells us if an object has derived columns generated by the database. +If the object does, this method should return true. If not, it can be left out. + +=cut + +sub _has_derived_columns { + return 0; +} + =head3 _type This method must be defined in the child class. The value is the name of the DBIC resultset. -- 2.15.1 (Apple Git-101)