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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 use List::MoreUtils qw( none );
30 Koha::Objects - Koha Object set base class
35 my @objects = Koha::Objects->search({ borrowernumber => $borrowernumber});
39 This class must be subclassed.
47 =head3 Koha::Objects->new();
49 my $object = Koha::Objects->new();
57 bless( $self, $class );
60 =head3 Koha::Objects->_new_from_dbic();
62 my $object = Koha::Objects->_new_from_dbic( $resultset );
67 my ( $class, $resultset ) = @_;
68 my $self = { _resultset => $resultset };
70 bless( $self, $class );
73 =head3 Koha::Objects->find();
75 Similar to DBIx::Class::ResultSet->find this method accepts:
76 \%columns_values | @pk_values, { key => $unique_constraint, %attrs }?
77 Strictly speaking, columns_values should only refer to columns under an
80 It returns undef if no results were found
82 my $object = Koha::Objects->find( { col1 => $val1, col2 => $val2 } );
83 my $object = Koha::Objects->find( $id );
84 my $object = Koha::Objects->find( $idpart1, $idpart2, $attrs ); # composite PK
89 my ( $self, @pars ) = @_;
93 unless (!@pars || none { defined($_) } @pars) {
94 my $result = $self->_resultset()->find(@pars);
96 $object = $self->object_class()->_new_from_dbic($result);
103 =head3 Koha::Objects->find_or_create();
105 my $object = Koha::Objects->find_or_create( $attrs );
110 my ( $self, $params ) = @_;
112 my $result = $self->_resultset->find_or_create($params);
114 return unless $result;
116 my $object = $self->object_class->_new_from_dbic($result);
121 =head3 Koha::Objects->search();
123 my @objects = Koha::Objects->search($params);
128 my ( $self, $params, $attributes ) = @_;
131 my @dbic_rows = $self->_resultset()->search($params, $attributes);
133 return $self->_wrap(@dbic_rows);
137 my $class = ref($self) ? ref($self) : $self;
138 my $rs = $self->_resultset()->search($params, $attributes);
140 return $class->_new_from_dbic($rs);
144 =head3 search_related
146 my @objects = Koha::Objects->search_related( $rel_name, $cond?, \%attrs? );
147 my $objects = Koha::Objects->search_related( $rel_name, $cond?, \%attrs? );
149 Searches the specified relationship, optionally specifying a condition and attributes for matching records.
154 my ( $self, $rel_name, @params ) = @_;
156 return if !$rel_name;
158 my @dbic_rows = $self->_resultset()->search_related($rel_name, @params);
159 return if !@dbic_rows;
160 my $object_class = _get_objects_class( $dbic_rows[0]->result_class );
162 eval "require $object_class";
163 return _wrap( $object_class, @dbic_rows );
166 my $rs = $self->_resultset()->search_related($rel_name, @params);
168 my $object_class = _get_objects_class( $rs->result_class );
170 eval "require $object_class";
171 return _new_from_dbic( $object_class, $rs );
182 if ( Class::Inspector->function_exists( $self->object_class, 'delete' ) ) {
184 $self->_resultset->result_source->schema->txn_do( sub {
185 $self->reset; # If we iterated already over the set
186 while ( my $o = $self->next ) {
191 return $objects_deleted;
194 return $self->_resultset->delete;
199 $object->update( $fields, [ { no_triggers => 0/1 } ] );
201 This method overloads the DBIC inherited one so if code-level triggers exist
202 (through the use of an overloaded I<update> or I<store> method in the Koha::Object
203 based class) those are called in a loop on the resultset.
205 If B<no_triggers> is passed and I<true>, then the DBIC update method is called
206 directly. This feature is important for performance, in cases where no code-level
207 triggers should be triggered. The developer will explicitly ask for this and QA should
208 catch wrong uses as well.
213 my ($self, $fields, $options) = @_;
215 my $no_triggers = $options->{no_triggers};
219 && ( Class::Inspector->function_exists( $self->object_class, 'update' )
220 or Class::Inspector->function_exists( $self->object_class, 'store' ) )
224 $self->_resultset->result_source->schema->txn_do( sub {
225 while ( my $o = $self->next ) {
230 return $objects_updated;
233 return $self->_resultset->update($fields);
238 my $object = Koha::Objects->search({}, { rows => 1 })->single
240 Returns one and only one object that is part of this set.
241 Returns undef if there are no objects found.
243 This is optimal as it will grab the first returned result without instantiating
247 http://search.cpan.org/dist/DBIx-Class/lib/DBIx/Class/Manual/Cookbook.pod#Retrieve_one_and_only_one_row_from_a_resultset
254 my $single = $self->_resultset()->single;
255 return unless $single;
257 return $self->object_class()->_new_from_dbic($single);
260 =head3 Koha::Objects->next();
262 my $object = Koha::Objects->next();
264 Returns the next object that is part of this set.
265 Returns undef if there are no more objects to return.
272 my $result = $self->_resultset()->next();
273 return unless $result;
275 my $object = $self->object_class()->_new_from_dbic( $result );
280 =head3 Koha::Objects->last;
282 my $object = Koha::Objects->last;
284 Returns the last object that is part of this set.
285 Returns undef if there are no object to return.
292 my $count = $self->_resultset->count;
293 return unless $count;
295 my ( $result ) = $self->_resultset->slice($count - 1, $count - 1);
297 my $object = $self->object_class()->_new_from_dbic( $result );
304 my $empty_rs = Koha::Objects->new->empty;
306 Sets the resultset empty. This is handy for consistency on method returns
307 (e.g. if we know in advance we won't have results but want to keep returning
315 unless (ref($self)) {
319 $self->_resultset()->set_cache([]);
324 =head3 Koha::Objects->reset();
326 Koha::Objects->reset();
328 resets iteration so the next call to next() will start agein
329 with the first object in a set.
336 $self->_resultset()->reset();
341 =head3 Koha::Objects->as_list();
343 Koha::Objects->as_list();
345 Returns an arrayref of the objects in this set.
352 my @dbic_rows = $self->_resultset()->all();
354 my @objects = $self->_wrap(@dbic_rows);
356 return wantarray ? @objects : \@objects;
359 =head3 Koha::Objects->unblessed
361 Returns an unblessed representation of objects.
368 return [ map { $_->unblessed } $self->as_list ];
371 =head3 Koha::Objects->get_column
373 Return all the values of this set for a given column
378 my ($self, $column_name) = @_;
379 return $self->_resultset->get_column( $column_name )->all;
382 =head3 Koha::Objects->TO_JSON
384 Returns an unblessed representation of objects, suitable for JSON output.
391 return [ map { $_->TO_JSON } $self->as_list ];
394 =head3 Koha::Objects->to_api
396 Returns a representation of the objects, suitable for API output .
401 my ($self, $params) = @_;
403 return [ map { $_->to_api($params) } $self->as_list ];
406 =head3 attributes_from_api
408 my $attributes = $objects->attributes_from_api( $api_attributes );
410 Translates attributes from the API to DBIC
414 sub attributes_from_api {
415 my ( $self, $attributes ) = @_;
417 $self->{_singular_object} ||= $self->object_class->new();
418 return $self->{_singular_object}->attributes_from_api( $attributes );
421 =head3 from_api_mapping
423 my $mapped_attributes_hash = $objects->from_api_mapping;
425 Attributes map from the API to DBIC
429 sub from_api_mapping {
432 $self->{_singular_object} ||= $self->object_class->new();
433 return $self->{_singular_object}->from_api_mapping;
436 =head3 prefetch_whitelist
438 my $whitelist = $object->prefetch_whitelist()
440 Returns a hash of prefetchable subs and the type it returns
444 sub prefetch_whitelist {
447 $self->{_singular_object} ||= $self->object_class->new();
449 $self->{_singular_object}->prefetch_whitelist;
452 =head3 Koha::Objects->_wrap
454 wraps the DBIC object in a corresponding Koha object
459 my ( $self, @dbic_rows ) = @_;
461 my @objects = map { $self->object_class->_new_from_dbic( $_ ) } @dbic_rows;
466 =head3 Koha::Objects->_resultset
468 Returns the internal resultset or creates it if undefined
476 $self->{_resultset} ||=
477 Koha::Database->new()->schema()->resultset( $self->_type() );
479 return $self->{_resultset};
482 return Koha::Database->new()->schema()->resultset( $self->_type() );
486 sub _get_objects_class {
490 if( $type->can('koha_objects_class') ) {
491 return $type->koha_objects_class;
493 $type =~ s|Schema::Result::||;
499 my @columns = Koha::Objects->columns
501 Return the table columns
507 return Koha::Database->new->schema->resultset( $class->_type )->result_source->columns;
512 The autoload method is used call DBIx::Class method on a resultset.
514 Important: If you plan to use one of the DBIx::Class methods you must provide
515 relevant tests in t/db_dependent/Koha/Objects.t
516 Currently count, is_paged, pager, result_class, single and slice are covered.
521 my ( $self, @params ) = @_;
523 my @known_methods = qw( count is_paged pager result_class single slice );
524 my $method = our $AUTOLOAD;
528 unless ( grep { $_ eq $method } @known_methods ) {
529 my $class = ref($self) ? ref($self) : $self;
530 Koha::Exceptions::Object::MethodNotCoveredByTests->throw(
531 error => sprintf("The method %s->%s is not covered by tests!", $class, $method),
536 my $r = eval { $self->_resultset->$method(@params) };
538 carp "No method $method found for " . ref($self) . " " . $@;
546 The _type method must be set for all child classes.
547 The value returned by it should be the DBIC resultset name.
548 For example, for holds, _type should return 'Reserve'.
556 This method must be set for all child classes.
557 The value returned by it should be the name of the Koha
558 object class that is returned by this class.
559 For example, for holds, object_class should return 'Koha::Hold'.
569 Kyle M Hall <kyle@bywatersolutions.com>