[Dbix-class] have add_to_$rel return $link for many to many

Todd W trwww at sbcglobal.net
Tue Aug 25 03:37:00 GMT 2009


Hello,

I'd like add_to_$rel return the $link instead of $obj in ::ManyToMany.pm. 
I've inlined a diff below that does so, with an updated test and 
documentation patch against 
http://dev.catalyst.perl.org/repos/bast/DBIx-Class/0.08/tags/0.08109

Index: t/relationship/core.t
===================================================================
--- t/relationship/core.t       (revision 7380)
+++ t/relationship/core.t       (working copy)
@@ -9,7 +9,7 @@
 my $schema = DBICTest->init_schema();
 my $sdebug = $schema->storage->debug;

-plan tests => 79;
+plan tests => 80;

 # has_a test
 my $cd = $schema->resultset("CD")->find(4);
@@ -185,7 +185,10 @@
 my $prod_before_count = $schema->resultset('Producer')->count;
 is( $prod_rs->count, 0, "CD doesn't yet have any producers" );
 my $prod = $schema->resultset('Producer')->find(1);
-$cd->add_to_producers($prod);
+isa_ok(
+  $cd->add_to_producers($prod),
+  'DBICTest::Schema::CD_to_Producer' => '$cd->add_to_producers($prod)'
+);
 is( $prod_rs->count(), 1, 'many_to_many add_to_$rel($obj) count ok' );
 is( $prod_rs->first->name, 'Matt S Trout',
     'many_to_many add_to_$rel($obj) ok' );
Index: lib/DBIx/Class/Relationship/ManyToMany.pm
===================================================================
--- lib/DBIx/Class/Relationship/ManyToMany.pm   (revision 7380)
+++ lib/DBIx/Class/Relationship/ManyToMany.pm   (working copy)
@@ -102,7 +102,7 @@
       my $link = $self->search_related($rel)->new_result($link_vals);
       $link->set_from_related($f_rel, $obj);
       $link->insert();
-         return $obj;
+         return $link;
     };

     my $set_meth_name = join '::', $class, $set_meth;
Index: lib/DBIx/Class/Relationship.pm
===================================================================
--- lib/DBIx/Class/Relationship.pm      (revision 7380)
+++ lib/DBIx/Class/Relationship.pm      (working copy)
@@ -595,13 +595,14 @@
 To add a role for your actor, and fill in the year of the role in the
 actor_roles table:

-  $actor->add_to_roles($role, { year => 1995 });
+  my $link = $actor->add_to_roles($role, { year => 1995 });

 In the above example, ActorRoles is the link table class, and Role is the
 foreign class. The C<$link_rel_name> parameter is the name of the accessor 
for
 the has_many relationship from this table to the link table, and the
 C<$foreign_rel_name> parameter is the accessor for the belongs_to 
relationship
-from the link table to the foreign table.
+from the link table to the foreign table. The My::DBIC::Schema::ActorRoles
+instance is returned.

 To use many_to_many, existing relationships from the original table to the 
link
 table, and from the link table to the end table must already exist, these 




More information about the DBIx-Class mailing list