I sent this question to the Class::DBI list last week, but didn't get
any replies so I thought I'd ask here too.  Can anyone help me out?

Kake

----- Forwarded message from Kate L Pugh <[EMAIL PROTECTED]> -----

Hello.  I only started using Class::DBI last week so please don't
laugh if this is a stupid question.

I'm using Class::DBI::Join to relate dealers (of the art kind) to
categories.  So I've got a (many-to-many) table that relates dealer
IDs and category IDs, and a Categorisation class that subclasses
Class::DBI::Join so I can put dealers in categories and get them out
again.  The thing I need to add is support for the 'importance' column
in that table - some dealers are major dealers in a category, others
only occasionally stock items from that category.  Now as it stands I
can do:

  $category->add_dealer( dealer => $minor_dealer, importance => 2 );

and the information goes in the table, and I can get all the dealers in
a category with:

  @dealers = Categorisation->join( $category ); 

but I want to be able to do:

  @dealers = Categorisation->join( $category, importance => 1 );

to get just the major dealers, and I can't work out what the
recommended way to do this is.  I made a patch to Class::DBI::Join
(attached with tests) to make the line above Just Work, but I'm sure
I've missed something that makes this a bad idea.

Help?


Kake


diff -pubr Class-DBI-Join-0.03/lib/Class/DBI/Join.pm 
class-dbi-join/lib/Class/DBI/Join.pm
--- Class-DBI-Join-0.03/lib/Class/DBI/Join.pm   2002-04-17 16:10:16.000000000 +0100
+++ class-dbi-join/lib/Class/DBI/Join.pm        2003-02-12 12:27:24.000000000 +0000
@@ -24,7 +24,8 @@ Class::DBI::Join - many-to-many relation
   # Given a join table like:
   # CREATE TABLE films_and_actors (
   #     film_id     INTEGER     REFERENCES films,
-  #     actor_id    INTEGER     REFERENCES actors
+  #     actor_id    INTEGER     REFERENCES actors,
+  #     as_name     VARCHAR(80)
   # );
   # And assuming Film and Actor are Class::DBI subclasses.
   Roles->table('films_and_actors');
@@ -40,6 +41,7 @@ Class::DBI::Join - many-to-many relation
 
   my $btaste = Film->retrieve('Bad Taste');
   my @roles  = Roles->join($btaste);
+  my @alien_roles = Roles->join($btaste, as_name => '3rd Class Alien');
 
 =head1 DESCRIPTION
 
@@ -57,8 +59,17 @@ Class::DBI::Join adds the following meth
 
 =item I<join>
 
+  # Find all roles in the film Bad Taste.
   my @roles = Roles->join($btaste);
 
+  # Or just those in which the actors are playing aliens.
+  my @alien_roles = Roles->join($btaste, as_name => '3rd Class Alien');
+
+The first argument to C<join> should be the object whose relations you
+want to find. Any other arguments will be passed straight through to
+the C<search> method of L<Class::DBI>, so you can provide information
+here to construct additional WHERE clauses.
+
 =cut
 
 __PACKAGE__->set_sql('ManyToMany', <<SQL);
@@ -68,10 +79,10 @@ WHERE   %s = ?
 SQL
 
 sub join {
-    my($class, $one) = @_;
+    my($class, $one, @args) = @_;
 
     my $hasa_cols    = __hasa_cols($class);
-    return $class->search( $hasa_cols->{ref $one} => $one->id );
+    return $class->search( $hasa_cols->{ref $one} => $one->id, @args );
 }
 
 
diff -pubr Class-DBI-Join-0.03/t/Join.t class-dbi-join/t/Join.t
--- Class-DBI-Join-0.03/t/Join.t        2002-04-16 21:09:03.000000000 +0100
+++ class-dbi-join/t/Join.t     2003-02-12 12:20:39.000000000 +0000
@@ -1,6 +1,6 @@
 #!/usr/bin/perl -w
 
-use Test::More tests => 11;
+use Test::More tests => 13;
 require_ok('Class::DBI::Join');
 
 use lib qw(t/lib/);
@@ -60,6 +60,12 @@ my $btaste = Film->retrieve('Bad Taste')
 my @bt_roles = Roles->join($btaste);
 is( @bt_roles, 6 );
 
+my @aliens = Roles->join($btaste, as_name => '3rd Class Alien');
+is( @aliens, 2, 'join works with extra where clause' );
+my @alien_actors = sort map { $_->actor->name } @aliens;
+is_deeply( \@alien_actors, ['Craig Smith (I)', 'Terry Potter'],
+          '...returns the right things too' );
+
 my $pj = Actor->retrieve('Peter Jackson');
 my @pj_roles = Roles->join($pj);
 is( @pj_roles, 4 );


----- End forwarded message -----

Reply via email to