use strict;
use warnings FATAL => 'all';
-sub retrieve { shift->find(@_) }
-sub retrieve_all { shift->search }
+
+sub retrieve {
+ die "No args to retrieve" unless @_ > 1;
+ shift->find(@_);
+}
sub retrieve_from_sql {
my ($class, $cond, @rest) = @_;
$class->search_literal($cond, @rest);
}
+sub retrieve_all { shift->search }
sub count_all { shift->count }
# Contributed by Numa. No test for this though.
sub find_related {
my $self = shift;
my $rel = shift;
+ return $self->search_related($rel)->find(@_);
+
+ # Marked for death.
my $rel_obj = $self->_relationships->{$rel};
$self->throw( "No such relationship ${rel}" ) unless $rel_obj;
my ($cond) = $self->resolve_condition($rel_obj->{cond}, { _action => 'convert' });
return $self->search(\$cond, $attrs);
}
+=head2 find(@colvalues), find(\%cols)
+
+Finds a row based on its primary key(s).
+
+=cut
+
+sub find {
+ my ($self, @vals) = @_;
+ my $attrs = (@vals > 1 && ref $vals[$#vals] eq 'HASH' ? pop(@vals) : {});
+ my @pk = $self->{source}->primary_columns;
+ #use Data::Dumper; warn Dumper($attrs, @vals, @pk);
+ $self->{source}->result_class->throw( "Can't find unless primary columns are defined" )
+ unless @pk;
+ my $query;
+ if (ref $vals[0] eq 'HASH') {
+ $query = $vals[0];
+ } elsif (@pk == @vals) {
+ $query = {};
+ @{$query}{@pk} = @vals;
+ } else {
+ $query = {@vals};
+ }
+ #warn Dumper($query);
+ # Useless -> disabled
+ #$self->{source}->result_class->throw( "Can't find unless all primary keys are specified" )
+ # unless (keys %$query >= @pk); # If we check 'em we run afoul of uc/lc
+ # column names etc. Not sure what to do yet
+ return $self->search($query)->next;
+}
+
=head2 search_related
$rs->search_related('relname', $cond?, $attrs?);
sub run_tests {
-plan tests => 3;
-
+plan tests => 4;
+$artist = DBICTest::Artist->find(1);
+ok($artist->find_related('twokeys', {cd => 1}), "find multiple pks using relationships + args");
ok(DBICTest->class("FourKeys")->find(1,2,3,4), "find multiple pks without hash");
ok(DBICTest->class("FourKeys")->find(5,4,3,6), "find multiple pks without hash");
-
is(DBICTest->class("FourKeys")->find(1,2,3,4)->ID, 'DBICTest::FourKeys|bar=2|foo=1|goodbye=4|hello=3', 'unique object id ok for multiple pks');
}