# with Koha; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-use overload "0+" => "count", "<>" => "next", fallback => 1;
-
use Modern::Perl;
use Carp;
=head3 Koha::Objects->new();
-my $object = Koha::Object->new();
+my $object = Koha::Objects->new();
=cut
bless( $self, $class );
}
-=head3 Koha::Objects->new_from_dbic();
+=head3 Koha::Objects->_new_from_dbic();
-my $object = Koha::Object->new_from_dbic( $resultset );
+my $object = Koha::Objects->_new_from_dbic( $resultset );
=cut
-sub new_from_dbic {
+sub _new_from_dbic {
my ( $class, $resultset ) = @_;
my $self = { _resultset => $resultset };
=head3 Koha::Objects->find();
-my $object = Koha::Object->find($id);
-my $object = Koha::Object->find( { keypart1 => $keypart1, keypart2 => $keypart2 } );
+my $object = Koha::Objects->find($id);
+my $object = Koha::Objects->find( { keypart1 => $keypart1, keypart2 => $keypart2 } );
=cut
sub find {
my ( $self, $id ) = @_;
+ return unless $id;
+
my $result = $self->_resultset()->find($id);
- my $object = $self->object_class()->new_from_dbic( $result );
+ return unless $result;
+
+ my $object = $self->object_class()->_new_from_dbic( $result );
return $object;
}
=head3 Koha::Objects->search();
-my @objects = Koha::Object->search($params);
+my @objects = Koha::Objects->search($params);
=cut
sub search {
- my ( $self, $params ) = @_;
+ my ( $self, $params, $attributes ) = @_;
if (wantarray) {
- my @dbic_rows = $self->_resultset()->search($params);
+ my @dbic_rows = $self->_resultset()->search($params, $attributes);
return $self->_wrap(@dbic_rows);
}
else {
- my $class = ref( $self );
- my $rs = $self->_resultset()->search($params);
+ my $class = ref($self) ? ref($self) : $self;
+ my $rs = $self->_resultset()->search($params, $attributes);
- return $class->new_from_dbic($rs);
+ return $class->_new_from_dbic($rs);
}
}
=head3 Koha::Objects->count();
-my @objects = Koha::Object->count($params);
+my @objects = Koha::Objects->count($params);
=cut
=head3 Koha::Objects->next();
-my $object = Koha::Object->next();
+my $object = Koha::Objects->next();
Returns the next object that is part of this set.
Returns undef if there are no more objects to return.
=cut
sub next {
- my ( $self, $id ) = @_;
+ my ( $self ) = @_;
my $result = $self->_resultset()->next();
return unless $result;
- my $object = $self->object_class()->new_from_dbic( $result );
+ my $object = $self->object_class()->_new_from_dbic( $result );
return $object;
}
=cut
sub reset {
- my ( $self, $id ) = @_;
+ my ( $self ) = @_;
$self->_resultset()->reset();
=cut
sub as_list {
- my ( $self, $id ) = @_;
+ my ( $self ) = @_;
my @dbic_rows = $self->_resultset()->all();
=head3 Koha::Objects->_wrap
-wraps the DBIC object in a corrosponding Koha object
+wraps the DBIC object in a corresponding Koha object
=cut
sub _wrap {
my ( $self, @dbic_rows ) = @_;
- my @objects = map { $self->object_class()->new_from_dbic( $_ ) } @dbic_rows;
+ my @objects = map { $self->object_class()->_new_from_dbic( $_ ) } @dbic_rows;
return @objects;
}
sub _resultset {
my ($self) = @_;
- $self->{_resultset} ||=
- Koha::Database->new()->schema()->resultset( $self->type() );
+ if ( ref($self) ) {
+ $self->{_resultset} ||=
+ Koha::Database->new()->schema()->resultset( $self->type() );
- $self->{_resultset};
+ return $self->{_resultset};
+ }
+ else {
+ return Koha::Database->new()->schema()->resultset( $self->type() );
+ }
}
=head3 type