View | Details | Raw Unified | Return to bug 17080
Collapse All | Expand All

(-)a/Koha/Object.pm (-2 / +11 lines)
Lines 56-63 sub new { Link Here
56
    my $self = {};
56
    my $self = {};
57
57
58
    if ($attributes) {
58
    if ($attributes) {
59
        $self->{_result} =
59
        my $schema = Koha::Database->new->schema;
60
          Koha::Database->new()->schema()->resultset( $class->_type() )
60
61
        # Remove the arguments which exist, are not defined but NOT NULL to use the default value
62
        my $columns_info = $schema->resultset( $class->_type )->result_source->columns_info;
63
        for my $column_name ( keys %$attributes ) {
64
            my $c_info = $columns_info->{$column_name};
65
            next if $c_info->{is_nullable};
66
            next if not exists $attributes->{$column_name} or defined $attributes->{$column_name};
67
            delete $attributes->{$column_name};
68
        }
69
        $self->{_result} = $schema->resultset( $class->_type() )
61
          ->new($attributes);
70
          ->new($attributes);
62
    }
71
    }
63
72
(-)a/t/db_dependent/Koha/Objects.t (-2 / +14 lines)
Lines 19-27 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 2;
22
use Test::More tests => 3;
23
23
24
use Koha::Authority::Types;
24
use Koha::Authority::Types;
25
use Koha::Patron::Category;
26
use Koha::Patron::Categories;
25
use Koha::Patrons;
27
use Koha::Patrons;
26
use Koha::Database;
28
use Koha::Database;
27
29
Lines 36-40 my @columns = Koha::Patrons->columns; Link Here
36
my $borrowernumber_exists = grep { /^borrowernumber$/ } @columns;
38
my $borrowernumber_exists = grep { /^borrowernumber$/ } @columns;
37
is( $borrowernumber_exists, 1, 'Koha::Objects->columns should return the table columns' );
39
is( $borrowernumber_exists, 1, 'Koha::Objects->columns should return the table columns' );
38
40
41
subtest 'new' => sub {
42
    plan tests => 2;
43
    my $a_cat_code = 'A_CAT_CODE';
44
    my $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code } )->store;
45
    is( Koha::Patron::Categories->find($a_cat_code)->category_type, 'A', 'Koha::Object->new should set the default value' );
46
    Koha::Patron::Categories->find($a_cat_code)->delete;
47
    $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code, category_type => undef } )->store;
48
    is( Koha::Patron::Categories->find($a_cat_code)->category_type, 'A', 'Koha::Object->new should set the default value even if the argument exists but is not defined' );
49
    Koha::Patron::Categories->find($a_cat_code)->delete;
50
};
51
39
$schema->storage->txn_rollback;
52
$schema->storage->txn_rollback;
40
1;
53
1;
41
- 

Return to bug 17080