Merge 'DBIx-Class-current' into 'many_to_many'
Matt S Trout [Sat, 17 Jun 2006 02:17:58 +0000 (02:17 +0000)]
r9175@cain (orig r1889):  matthewt | 2006-05-30 19:30:52 +0000
fixed debugfh
r9176@cain (orig r1890):  gphat | 2006-05-30 21:05:56 +0000
Fix column_info stomping, test coming...

r9177@cain (orig r1891):  matthewt | 2006-05-30 21:09:25 +0000
nuke uuid test (uuid not in core no more, lenore)
r9198@cain (orig r1892):  gphat | 2006-05-30 23:53:55 +0000
Test for earlier column_info overwriting (r1890)

r9199@cain (orig r1893):  nigel | 2006-05-31 12:13:21 +0000
Initial work on getting POD coverage testing working
r9200@cain (orig r1894):  nigel | 2006-05-31 21:58:16 +0000
POD::Coverage additions
r9203@cain (orig r1897):  phaylon | 2006-06-01 15:16:18 +0000
Test if value is blessed before ->isa
r9226@cain (orig r1899):  tomk | 2006-06-02 19:14:43 +0000
Moved Validation.pm from DBIC-current into DBIx-Class-Validate

r9889@cain (orig r1918):  captainL | 2006-06-05 17:58:38 +0000
resultset bugfix changes
r9893@cain (orig r1922):  blblack | 2006-06-06 00:14:28 +0000
0.06999_01 Changes fixup (forgotten entry for connect_info stuff)
r9897@cain (orig r1926):  blblack | 2006-06-06 12:30:40 +0000
POD clarification and content bugfixing + a few code formatting fixes
r9898@cain (orig r1927):  blblack | 2006-06-06 12:31:03 +0000
new specific test for connect_info coderefs
r9899@cain (orig r1928):  bluefeet | 2006-06-06 14:21:03 +0000
Slight wording change to new_related() POD.
r9900@cain (orig r1929):  captainL | 2006-06-06 15:03:46 +0000
fixed search with joins from related resultset
r9901@cain (orig r1930):  dwc | 2006-06-06 15:46:11 +0000
Revert change to test for deprecated find usage and swallow warnings
r9905@cain (orig r1934):  jguenther | 2006-06-06 22:02:34 +0000
Updated docs to reflect the addition of PK::Auto to load_components

r9906@cain (orig r1935):  jguenther | 2006-06-06 22:29:14 +0000
Fixed 'DBIx/Class/DB.pm did not return a true value' error

r9907@cain (orig r1936):  jguenther | 2006-06-06 22:48:05 +0000
Added code & tests for warning about incorrect component order in load_components. e.g., load_components(qw/Core UTF8Columns/)
r9908@cain (orig r1937):  jguenther | 2006-06-06 23:06:59 +0000
added last patch to Changes

r9910@cain (orig r1939):  captainL | 2006-06-06 23:50:48 +0000
 r759@grumpyjack (orig r1932):  captainL | 2006-06-06 21:48:37 +0100
 tightened up deep search_related

r9911@cain (orig r1940):  captainL | 2006-06-06 23:53:40 +0000
 r764@grumpyjack (orig r1938):  captainL | 2006-06-07 00:49:10 +0100
 branch cleanage

r9913@cain (orig r1942):  gphat | 2006-06-07 21:01:47 +0000
Fix typo in warning

r9916@cain (orig r1945):  captainL | 2006-06-07 23:39:10 +0000
 r767@grumpyjack (orig r1941):  captainL | 2006-06-07 17:30:49 +0100
 prefetch bugfix work-in-progress

r9917@cain (orig r1946):  captainL | 2006-06-07 23:39:19 +0000
 r769@grumpyjack (orig r1943):  captainL | 2006-06-08 00:31:44 +0100
 obscure prefetch problem fixed

r9918@cain (orig r1947):  captainL | 2006-06-07 23:39:32 +0000

r9919@cain (orig r1948):  blblack | 2006-06-08 03:28:24 +0000
bugfix for join-types in nested joins using the from attribute (+ test)
r9920@cain (orig r1949):  captainL | 2006-06-08 09:29:43 +0000
stopped test relying on order of unordered search
r9925@cain (orig r1954):  jguenther | 2006-06-08 22:47:58 +0000
Added `use' statement for DBD::Pg

r9926@cain (orig r1955):  blblack | 2006-06-09 18:19:33 +0000
only rebless S::DBI if it is still S::DBI and not a subclass
r9927@cain (orig r1956):  blblack | 2006-06-09 19:45:28 +0000
NoBindVars + Sybase + MSSQL stuff
r9928@cain (orig r1957):  jguenther | 2006-06-09 21:04:50 +0000
added code and tests for Componentized::ensure_class_found and load_optional_class
r9929@cain (orig r1958):  jguenther | 2006-06-09 21:38:58 +0000
Updated version and Changes for 0.06999_02
r9930@cain (orig r1959):  ningu | 2006-06-09 22:36:40 +0000
fix for prefetch bug in _merge_attr
r9931@cain (orig r1960):  ningu | 2006-06-09 22:37:33 +0000
 r12287@haferschleim (orig r1900):  jester | 2006-06-02 21:28:09 +0200
 fixed trivial perldoc error
 r12293@haferschleim (orig r1906):  castaway | 2006-06-03 13:18:55 +0200
 Added "The DBIX::Class way" by Adam Jacob

 r12508@haferschleim (orig r1952):  castaway | 2006-06-08 14:55:46 +0200
 Fix insert overloading example

r9932@cain (orig r1961):  jguenther | 2006-06-09 22:45:54 +0000
formatting fixes for ResultSet.pm
r9933@cain (orig r1962):  matthewt | 2006-06-09 23:07:17 +0000
timestamp. yes I missed it for the dist.
r9934@cain (orig r1963):  matthewt | 2006-06-10 00:55:46 +0000
feh
r10031@cain (orig r1964):  blblack | 2006-06-11 21:25:59 +0000
bugfix: must set connect_info properly before setting any ->sql_maker things
r10033@cain (orig r1966):  blblack | 2006-06-12 13:41:58 +0000
author list correction/update
r10034@cain (orig r1967):  nigel | 2006-06-12 14:02:49 +0000
Re-enabled POD::Coverage tests as they are now running cleanly
r10035@cain (orig r1968):  jguenther | 2006-06-12 16:05:07 +0000
fixed wrong debugging hook call query_begin() to query_start() in Storage::DBI
r10036@cain (orig r1969):  jguenther | 2006-06-12 16:11:21 +0000
Fixed incorrect ordering of POD sections

r10037@cain (orig r1970):  castaway | 2006-06-14 21:23:29 +0000
Fix documentation of search_related

r10043@cain (orig r1971):  blblack | 2006-06-16 20:45:16 +0000
further changes to the Sybase / MSSQL storage stuff
r10044@cain (orig r1972):  matthewt | 2006-06-16 21:54:53 +0000
fuckheaded failure to check defined before regexing
r10045@cain (orig r1973):  matthewt | 2006-06-16 23:02:57 +0000
probably added date support

lib/DBIx/Class/Relationship/ManyToMany.pm
t/66relationship.t

index 387fc0b..a88e1b0 100644 (file)
@@ -9,11 +9,36 @@ sub many_to_many {
   {
     no strict 'refs';
     no warnings 'redefine';
+
     *{"${class}::${meth}"} = sub {
       my $self = shift;
       my $attrs = @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {};
-      $self->search_related($rel)->search_related($f_rel, @_ > 0 ? @_ : undef, { %{$rel_attrs||{}}, %$attrs });
+      $self->search_related($rel)->search_related(
+        $f_rel, @_ > 0 ? @_ : undef, { %{$rel_attrs||{}}, %$attrs }
+      );
+    };
+
+    *{"${class}::add_to_${meth}"} = sub {
+      my( $self, $obj ) = @_;
+      my $vals = @_ > 2 && ref $_[$#_] eq 'HASH' ? pop(@_) : {};
+      return $self->search_related($rel)->create({
+        map { $_=>$self->get_column($_) } $self->primary_columns(),
+        map { $_=>$obj->get_column($_) } $obj->primary_columns(),
+        %$vals,
+      });
     };
+
+    *{"${class}::remove_from_${meth}"} = sub {
+      my( $self, $obj ) = @_;
+      return $self->search_related(
+        $rel,
+        {
+            map { $_=>$self->get_column($_) } $self->primary_columns(),
+            map { $_=>$obj->get_column($_) } $obj->primary_columns(),
+        },
+      )->delete();
+    };
+
   }
 }
 
index dce68f7..ac4c2de 100644 (file)
@@ -7,7 +7,7 @@ use DBICTest;
 
 my $schema = DBICTest->init_schema();
 
-plan tests => 32;
+plan tests => 35;
 
 # has_a test
 my $cd = $schema->resultset("CD")->find(4);
@@ -134,6 +134,16 @@ is( $producers[0]->name, 'Matt S Trout', 'many_to_many ok' );
 is( $cd->producers_sorted->next->name, 'Bob The Builder', 'sorted many_to_many ok' );
 is( $cd->producers_sorted(producerid => 3)->next->name, 'Fred The Phenotype', 'sorted many_to_many with search condition ok' );
 
+# test new many_to_many helpers
+$cd = $schema->resultset('CD')->find(2);
+my $prod = $schema->resultset('Producer')->find(1);
+$cd->add_to_producers($prod);
+my $prod_rs = $cd->producers();
+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' );
+$cd->remove_from_producers($prod);
+is( $cd->producers->count, 0, 'many_to_many remove_from_$rel($obj) ok' );
+
 # test undirected many-to-many relationship (e.g. "related artists")
 my $undir_maps = $schema->resultset("Artist")->find(1)->artist_undirected_maps;
 is($undir_maps->count, 1, 'found 1 undirected map for artist 1');