3 # Copyright ByWater Solutions 2014
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 use Encode qw{encode};
29 Koha::Object - Koha Object base class
34 my $object = Koha::Object->new({ property1 => $property1, property2 => $property2, etc... } );
38 This class must always be subclassed.
46 =head3 Koha::Object->new();
48 my $object = Koha::Object->new();
49 my $object = Koha::Object->new($attributes);
54 my ( $class, $attributes ) = @_;
59 Koha::Database->new()->schema()->resultset( $class->type() )
63 croak("No type found! Koha::Object must be subclassed!")
64 unless $class->type();
66 bless( $self, $class );
70 =head3 Koha::Object->new_from_dbic();
72 my $object = Koha::Object->new_from_dbic($dbic_row);
77 my ( $class, $dbic_row ) = @_;
81 $self->{_result} = $dbic_row;
83 croak("No type found! Koha::Object must be subclassed!")
84 unless $class->type();
86 croak( "DBIC result type " . ref( $self->{_result} ) . " isn't of the type " . $class->type() )
87 unless ref( $self->{_result} ) eq "Koha::Schema::Result::" . $class->type();
89 bless( $self, $class );
93 =head3 $object->store();
95 Saves the object in storage.
96 If the object is new, it will be created.
97 If the object previously existed, it will be updated.
100 1 if the store was a success
101 0 if the store failed
108 return $self->_result()->update_or_insert() ? 1 : 0;
111 =head3 $object->in_storage();
113 Returns true if the object has been previously stored.
120 return $self->_result()->in_storage();
123 =head3 $object->is_changed();
125 Returns true if the object has properties that are different from
126 the properties of the object in storage.
131 my ( $self, @columns ) = @_;
133 return $self->_result()->is_changed(@columns);
136 =head3 $object->delete();
138 Removes the object from storage.
141 1 if the deletion was a success
142 0 if the deletion failed
143 -1 if the object was never in storage
150 # Deleting something not in storage thows an exception
151 return -1 unless $self->_result()->in_storage();
153 # Return a boolean for succcess
154 return $self->_result()->delete() ? 1 : 0;
157 =head3 $object->set( $properties_hashref )
161 property1 => $property1,
162 property2 => $property2,
163 property3 => $propery3,
167 Enables multiple properties to be set at once
170 1 if all properties were set.
171 0 if one or more properties do not exist.
172 undef if all properties exist but a different error
173 prevents one or more properties from being set.
175 If one or more of the properties do not exist,
176 no properties will be set.
181 my ( $self, $properties ) = @_;
183 my @columns = @{$self->_columns()};
185 foreach my $p ( keys %$properties ) {
186 unless ( $p ~~ @columns ) {
187 carp("No property $p!");
192 return $self->_result()->set_columns($properties) ? 1 : undef;
195 =head3 $object->id();
197 Returns the id of the object if it has one.
204 my ( $id ) = $self->_result()->id();
209 =head3 $object->_result();
211 Returns the internal DBIC Row object
218 # If we don't have a dbic row at this point, we need to create an empty one
220 Koha::Database->new()->schema()->resultset( $self->type() )->new({});
222 return $self->{_result};
225 =head3 $object->_columns();
227 Returns an arrayref of the table columns
234 # If we don't have a dbic row at this point, we need to create an empty one
235 $self->{_columns} ||= [ $self->_result()->result_source()->columns() ];
237 return $self->{_columns};
243 The autoload method is used only to get and set values for an objects properties.
250 my $method = our $AUTOLOAD;
253 my @columns = @{$self->_columns()};
254 # Using direct setter/getter like $item->barcode() or $item->barcode($barcode);
255 if ( $method ~~ @columns ) {
257 return $self->_result()->set_column( $method, @_ );
259 my $value = $self->_result()->get_column( $method );
260 return encode( 'UTF-8', $value );
264 carp "No method $method!";
270 This method must be defined in the child class. The value is the name of the DBIC resultset.
271 For example, for borrowers, the type method will return "Borrower".
281 Kyle M Hall <kyle@bywatersolutions.com>