sub store {
my ($self) = @_;
+ my $columns_info = Koha::Database->new->schema->resultset( $self->_type )
+ ->result_source->{_columns};
+
+ # Handle not null and default values for integers and dates
+ foreach my $col ( keys %{$columns_info} ) {
+ # Integers
+ if ( _numeric_column_type( $columns_info->{$col}->{data_type} ) ) {
+ # Has been passed but not a number, usually an empty string
+ if ( defined $self->$col and not looks_like_number( $self->$col ) ) {
+ if ( $columns_info->{$col}->{is_nullable} ) {
+ # If nullable, default to null
+ $self->$col(undef);
+ } else {
+ # If cannot be null, get the default value
+ # What if cannot be null and does not have a default value? Possible?
+ $self->$col($columns_info->{$col}->{default_value});
+ }
+ }
+ }
+ elsif ( _date_or_datetime_column_type( $columns_info->{$col}->{data_type} ) ) {
+ # Set to null if an empty string (or == 0 but should not happen)
+ $self->$col(undef) unless $self->$col;
+ }
+ }
+
try {
return $self->_result()->update_or_insert() ? $self : undef;
}
return $unblessed;
}
+sub _date_or_datetime_column_type {
+ my ($column_type) = @_;
+
+ my @dt_types = (
+ 'timestamp',
+ 'date',
+ 'datetime'
+ );
+
+ return ( grep { $column_type eq $_ } @dt_types) ? 1 : 0;
+}
sub _datetime_column_type {
my ($column_type) = @_;