#
# This file is part of Koha.
#
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 3 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
#
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
#
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
Strictly speaking, columns_values should only refer to columns under an
unique constraint.
+It returns undef if no results were found
+
my $object = Koha::Objects->find( { col1 => $val1, col2 => $val2 } );
my $object = Koha::Objects->find( $id );
my $object = Koha::Objects->find( $idpart1, $idpart2, $attrs ); # composite PK
sub find {
my ( $self, @pars ) = @_;
- croak 'Cannot use "->find" in list context' if wantarray;
-
- return if !@pars || none { defined($_) } @pars;
-
- my $result = $self->_resultset()->find( @pars );
+ my $object;
- return unless $result;
-
- my $object = $self->object_class()->_new_from_dbic( $result );
+ unless (!@pars || none { defined($_) } @pars) {
+ my $result = $self->_resultset()->find(@pars);
+ if ($result) {
+ $object = $self->object_class()->_new_from_dbic($result);
+ }
+ }
return $object;
}
return [ map { $_->TO_JSON } $self->as_list ];
}
+=head3 Koha::Objects->to_api
+
+Returns a representation of the objects, suitable for API output .
+
+=cut
+
+sub to_api {
+ my ($self, $params) = @_;
+
+ return [ map { $_->to_api($params) } $self->as_list ];
+}
+
+=head3 attributes_from_api
+
+ my $attributes = $objects->attributes_from_api( $api_attributes );
+
+Translates attributes from the API to DBIC
+
+=cut
+
+sub attributes_from_api {
+ my ( $self, $attributes ) = @_;
+
+ $self->{_singular_object} ||= $self->object_class->new();
+ return $self->{_singular_object}->attributes_from_api( $attributes );
+}
+
+=head3 from_api_mapping
+
+ my $mapped_attributes_hash = $objects->from_api_mapping;
+
+Attributes map from the API to DBIC
+
+=cut
+
+sub from_api_mapping {
+ my ( $self ) = @_;
+
+ $self->{_singular_object} ||= $self->object_class->new();
+ return $self->{_singular_object}->from_api_mapping;
+}
+
+=head3 prefetch_whitelist
+
+ my $whitelist = $object->prefetch_whitelist()
+
+Returns a hash of prefetchable subs and the type it returns
+
+=cut
+
+sub prefetch_whitelist {
+ my ( $self ) = @_;
+
+ $self->{_singular_object} ||= $self->object_class->new();
+
+ $self->{_singular_object}->prefetch_whitelist;
+}
+
=head3 Koha::Objects->_wrap
wraps the DBIC object in a corresponding Koha object
sub AUTOLOAD {
my ( $self, @params ) = @_;
- my @known_methods = qw( count pager update delete result_class single slice );
+ my @known_methods = qw( count is_paged pager update delete result_class single slice );
my $method = our $AUTOLOAD;
$method =~ s/.*:://;
- carp "The method $method is not covered by tests" and return unless grep {/^$method$/} @known_methods;
+
+ unless ( grep { $_ eq $method } @known_methods ) {
+ my $class = ref($self) ? ref($self) : $self;
+ Koha::Exceptions::Object::MethodNotCoveredByTests->throw(
+ error => sprintf("The method %s->%s is not covered by tests!", $class, $method),
+ show_trace => 1
+ );
+ }
+
my $r = eval { $self->_resultset->$method(@params) };
if ( $@ ) {
carp "No method $method found for " . ref($self) . " " . $@;