]> Dogcows Code - chaz/p5-DBIx-Class-ResultSet-RecursiveUpdate/commitdiff
handle many-to-many pseudo rels separately
authorAlexander Hartmaier <abraxxa@cpan.org>
Sat, 23 Oct 2010 14:17:58 +0000 (16:17 +0200)
committerAlexander Hartmaier <abraxxa@cpan.org>
Sat, 23 Oct 2010 14:17:58 +0000 (16:17 +0200)
lib/DBIx/Class/ResultSet/RecursiveUpdate.pm

index 965fa1c385a5a0c2aa90888799dc3c936ff86eb3..59dbc47311d5368778107044ec6eddd81e3a1250 100644 (file)
@@ -97,6 +97,7 @@ sub recursive_update {
     # database like has_many, might_have and has_one
     my %post_updates;
     my %other_methods;
+    my %m2m_accessors;
     my %columns_by_accessor = _get_columns_by_accessor($self);
 
     #    warn 'resolved: ' . Dumper( $resolved );
@@ -134,7 +135,7 @@ sub recursive_update {
         if ( is_m2m( $self, $name ) ) {
 
             #warn "$name is a many-to-many helper accessor\n";
-            $other_methods{$name} = $updates->{$name};
+            $m2m_accessors{$name} = $updates->{$name};
             next;
         }
 
@@ -190,42 +191,40 @@ sub recursive_update {
     $object->discard_changes;
 
     # updating many_to_many
-    for my $name ( keys %$updates ) {
-        next if exists $columns{$name};
-        my $value = $updates->{$name};
-
-        if ( is_m2m( $self, $name ) ) {
-
-            #warn "update m2m $name\n";
-            my ($pk) = _get_pk_for_related( $self, $name );
-            my @rows;
-            my $result_source = $object->$name->result_source;
-            my @updates;
-            if ( !defined $value ) {
-                next;
-            }
-            elsif ( ref $value ) {
-                @updates = @{$value};
+    for my $name ( keys %m2m_accessors ) {
+        my $value = $m2m_accessors{$name};
+
+        #warn "update m2m $name\n";
+        # TODO: only first pk col is used
+        my ($pk) = _get_pk_for_related( $self, $name );
+        my @rows;
+        my $result_source = $object->$name->result_source;
+        my @updates;
+        if ( !defined $value ) {
+            #next;
+            @updates = ();
+        }
+        elsif ( ref $value ) {
+            @updates = @{$value};
+        }
+        else {
+            @updates = ($value);
+        }
+        for my $elem (@updates) {
+            if ( ref $elem ) {
+                push @rows, $elem;
+                    # recursive_update(
+                    # resultset => $result_source->resultset,
+                    # updates   => $elem
+                    # );
             }
             else {
-                @updates = ($value);
-            }
-            for my $elem (@updates) {
-                if ( ref $elem ) {
-                    push @rows,
-                        recursive_update(
-                        resultset => $result_source->resultset,
-                        updates   => $elem
-                        );
-                }
-                else {
-                    push @rows,
-                        $result_source->resultset->find( { $pk => $elem } );
-                }
+                push @rows,
+                    $result_source->resultset->find( { $pk => $elem } );
             }
-            my $set_meth = 'set_' . $name;
-            $object->$set_meth( \@rows );
         }
+        my $set_meth = 'set_' . $name;
+        $object->$set_meth( \@rows );
     }
     for my $name ( keys %post_updates ) {
 
This page took 0.021601 seconds and 4 git commands to generate.