Bug 18361: Koha::Objects->find should accept composite primary keys
authorMarcel de Rooy <m.de.rooy@rijksmuseum.nl>
Fri, 31 Mar 2017 11:52:15 +0000 (13:52 +0200)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Fri, 16 Jun 2017 14:52:18 +0000 (11:52 -0300)
Changes the $id parameter to an array. (IssuingRule has three keys.)
The build_object method in TestBuilder.pm has been adjusted to pass
multiple primary key values to find.

Also adjusted the POD section to show more clearly that we accept
the same parameters as DBIx ResultSet does.

Test plan:
Run t/db_dependent/Koha/Object.t
Run t/db_dependent/Koha/Objects.t
Run t/db_dependent/TestBuilder.t

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Koha/Objects.pm
t/lib/TestBuilder.pm

index 5080e7d..ff1a7e5 100644 (file)
@@ -70,19 +70,25 @@ sub _new_from_dbic {
 
 =head3 Koha::Objects->find();
 
-my $object = Koha::Objects->find($id);
-my $object = Koha::Objects->find( { keypart1 => $keypart1, keypart2 => $keypart2 } );
+Similar to DBIx::Class::ResultSet->find this method accepts:
+    \%columns_values | @pk_values, { key => $unique_constraint, %attrs }?
+Strictly speaking, columns_values should only refer to columns under an
+unique constraint.
+
+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
 
 =cut
 
 sub find {
-    my ( $self, $id ) = @_;
+    my ( $self, @pars ) = @_;
 
     croak 'Cannot use "->find" in list context' if wantarray;
 
-    return unless defined($id);
+    return unless @pars;
 
-    my $result = $self->_resultset()->find($id);
+    my $result = $self->_resultset()->find( @pars );
 
     return unless $result;
 
index 6509aee..ce8f601 100644 (file)
@@ -74,7 +74,7 @@ sub build_object {
     my @ids;
 
     foreach my $pk ( @pks ) {
-        push @ids, { $pk => $hashref->{ $pk } };
+        push @ids, $hashref->{ $pk };
     }
 
     my $object = $class->find( @ids );