[Dbix-class] [patch] new DBIx::Class::Relationship::Helpers introducing many_to_many

Hartmaier Alexander Alexander.Hartmaier at t-systems.at
Fri Dec 9 17:48:33 CET 2005


This is mainly for Matt who should have an eye on it to put it into trunk 
after i (of matt ;-) add some error checking and docs.

Syntax is:

__PACKAGE__->many_to_many( 'methodname', 
'My::Model::Table::ManyToManyRelationshipTable', 
'has_many_name_relationship_name', 
'foreign_key_field_name_of_the_many_to_many_table' );

More too come next week!

Have a nice weekend and take care,
-Alex

*snip*
svn diff
Index: lib/DBIx/Class/Relationship/Helpers.pm
===================================================================
--- lib/DBIx/Class/Relationship/Helpers.pm      (revision 0)
+++ lib/DBIx/Class/Relationship/Helpers.pm      (revision 0)
@@ -0,0 +1,30 @@
+package DBIx::Class::Relationship::Helpers;
+
+use strict;
+use warnings;
+
+sub many_to_many {
+  my ($class, $rel, $f_class, $f_rel, $f_key) = @_;
+  my %meth;
+
+  eval "require $f_class";
+
+  $meth{$rel} = sub {
+    my ($self) = @_;
+
+    my $curr_class = $self->resolve_class($f_class);
+
+    return $curr_class->search(
+      { "$f_rel.$f_key" => $self->id },
+      { join => $f_rel });
+  };
+  {
+    no strict 'refs';
+    no warnings 'redefine';
+    foreach my $meth (keys %meth) {
+      *{"${class}::${meth}"} = $meth{$meth};
+    }
+  }
+}
+
+1;
Index: lib/DBIx/Class/Relationship.pm
===================================================================
--- lib/DBIx/Class/Relationship.pm      (revision 356)
+++ lib/DBIx/Class/Relationship.pm      (working copy)
@@ -13,6 +13,7 @@
   CascadeActions
   ProxyMethods
   Base
+  Helpers
 /);

 __PACKAGE__->mk_classdata('_relationships', { } );
*snip*


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 5544 bytes
Desc: not available
Url : http://lists.rawmode.org/pipermail/dbix-class/attachments/20051209/2661af76/smime.bin


More information about the Dbix-class mailing list