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

(-)a/Koha/Object.pm (+26 lines)
Lines 92-97 sub _new_from_dbic { Link Here
92
92
93
}
93
}
94
94
95
=head3 find
96
97
    my $object = Koha::Object->find( $id );
98
99
    This method allows you to fetch record $id from the database.
100
    It would normally be used as a static (class) method, but it can be
101
    called too as an object method.
102
    It returns a new object or undef if $id is not found.
103
104
=cut
105
106
sub find {
107
    my ( $class_or_self, $id ) = @_;
108
    my $class;
109
    if( ref $class_or_self ) {
110
        $class = $class_or_self->type;
111
    } else {
112
        $class = $class_or_self;
113
        $class =~ s/^Koha:://;
114
    }
115
    my $result =
116
        Koha::Database->new()->schema()->resultset( $class )->find($id);
117
    return if !$result;
118
    return _new_from_dbic( "Koha::$class", $result );
119
}
120
95
=head3 $object->store();
121
=head3 $object->store();
96
122
97
Saves the object in storage.
123
Saves the object in storage.
(-)a/t/db_dependent/Borrower.t (-4 / +8 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 13;
20
use Test::More tests => 14;
21
use Test::Warn;
21
use Test::Warn;
22
22
23
use C4::Context;
23
use C4::Context;
Lines 49-57 is( $object->in_storage, 1, "Object is now stored" ); Link Here
49
49
50
my $borrowernumber = $object->borrowernumber;
50
my $borrowernumber = $object->borrowernumber;
51
51
52
my $borrower = Koha::Database->new()->schema()->resultset('Borrower')->find( $borrowernumber );
52
#test find as class method on the stored borrower
53
my $borrower = Koha::Borrower->find( $borrowernumber );
53
is( $borrower->surname(), "Test Surname", "Object found in database" );
54
is( $borrower->surname(), "Test Surname", "Object found in database" );
54
55
56
#test find as an object method too
57
is( ref $borrower->find( $borrowernumber ), 'Koha::Borrower',
58
    'Find as object method works too');
59
55
is( $object->is_changed(), 0, "Object is unchanged" );
60
is( $object->is_changed(), 0, "Object is unchanged" );
56
$object->surname("Test Surname");
61
$object->surname("Test Surname");
57
is( $object->is_changed(), 0, "Object is still unchanged" );
62
is( $object->is_changed(), 0, "Object is still unchanged" );
Lines 67-73 $object->store(); Link Here
67
is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
72
is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
68
73
69
$object->delete();
74
$object->delete();
70
$borrower = Koha::Database->new()->schema()->resultset('Borrower')->find( $borrowernumber );
75
$borrower = Koha::Borrower->find( $borrowernumber );
71
ok( ! $borrower, "Object no longer found in database" );
76
ok( ! $borrower, "Object no longer found in database" );
72
is( $object->in_storage, 0, "Object is not in storage" );
77
is( $object->in_storage, 0, "Object is not in storage" );
73
78
74
- 

Return to bug 13019