From: Peter Rabbitson Date: Sat, 6 Jun 2009 08:50:56 +0000 (+0000) Subject: Port another forgotten MC fix X-Git-Tag: v0.08106~31 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=577ef680ceebff618b85a41dec6177074f51b76c;p=dbsrgits%2FDBIx-Class.git Port another forgotten MC fix --- diff --git a/lib/DBIx/Class/Relationship/ManyToMany.pm b/lib/DBIx/Class/Relationship/ManyToMany.pm index e953a44..bab7bb1 100644 --- a/lib/DBIx/Class/Relationship/ManyToMany.pm +++ b/lib/DBIx/Class/Relationship/ManyToMany.pm @@ -85,12 +85,12 @@ EOW my $obj; if (ref $_[0]) { if (ref $_[0] eq 'HASH') { - $obj = $f_rel_rs->create($_[0]); + $obj = $f_rel_rs->find_or_create($_[0]); } else { $obj = $_[0]; } } else { - $obj = $f_rel_rs->create({@_}); + $obj = $f_rel_rs->find_or_create({@_}); } my $link_vals = @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {}; diff --git a/t/multi_create/m2m.t b/t/multi_create/m2m.t new file mode 100644 index 0000000..4275f58 --- /dev/null +++ b/t/multi_create/m2m.t @@ -0,0 +1,30 @@ +use strict; +use warnings; + +use Test::More; +use Test::Exception; +use lib qw(t/lib); +use DBICTest; + +plan tests => 4; + +my $schema = DBICTest->init_schema(); + +lives_ok ( sub { + + my $prod_rs = $schema->resultset ('Producer'); + my $prod_count = $prod_rs->count; + + my $cd = $schema->resultset('CD')->first; + $cd->add_to_producers ({name => 'new m2m producer'}); + + is ($prod_rs->count, $prod_count + 1, 'New producer created'); + ok ($cd->producers->find ({name => 'new m2m producer'}), 'Producer created with correct name'); + + my $cd2 = $schema->resultset('CD')->search ( { cdid => { '!=', $cd->cdid } }, {rows => 1} )->single; # retrieve a cd different from the first + $cd2->add_to_producers ({name => 'new m2m producer'}); # attach to an existing producer + ok ($cd2->producers->find ({name => 'new m2m producer'}), 'Exsiting producer attached to existing cd'); + +}, 'Test far-end find_or_create over many_to_many'); + +1;