]> Dogcows Code - chaz/p5-DBIx-Class-ResultSet-RecursiveUpdate/commitdiff
fixed_fields as array
authorzby <zby@bd8105ee-0ff8-0310-8827-fb3f25b6796d>
Mon, 16 Feb 2009 12:45:27 +0000 (12:45 +0000)
committerzby <zby@bd8105ee-0ff8-0310-8827-fb3f25b6796d>
Mon, 16 Feb 2009 12:45:27 +0000 (12:45 +0000)
lib/DBIx/Class/ResultSet/RecursiveUpdate.pm
t/lib/RunTests.pm
t/var/dvdzbr.db

index 59b2a6405a0db607b244454fe806b1b0284a4820..1c408252dff1cbaef14d3d587224963634d7ceda 100644 (file)
@@ -11,13 +11,16 @@ use base qw(DBIx::Class::ResultSet);
 
 sub recursive_update {
     my ( $self, $updates, $fixed_fields ) = @_;
 
 sub recursive_update {
     my ( $self, $updates, $fixed_fields ) = @_;
-
     # warn 'entering: ' . $self->result_source->from();
     # warn 'entering: ' . $self->result_source->from();
+
+    carp 'fixed fields needs to be an array ref' if $fixed_fields && ref($fixed_fields) ne 'ARRAY';
+    my %fixed_fields;
+    %fixed_fields = map { $_ => 1 } @$fixed_fields if $fixed_fields;
+
     if ( blessed($updates) && $updates->isa('DBIx::Class::Row') ) {
         return $updates;
     }
 
     if ( blessed($updates) && $updates->isa('DBIx::Class::Row') ) {
         return $updates;
     }
 
-    carp 'fixed fields needs to be a hash ref' if $fixed_fields && ref($fixed_fields) ne 'HASH';
 
     # direct column accessors
     my %columns;
 
     # direct column accessors
     my %columns;
@@ -31,7 +34,6 @@ sub recursive_update {
     my %post_updates;
     my %columns_by_accessor = $self->_get_columns_by_accessor;
 
     my %post_updates;
     my %columns_by_accessor = $self->_get_columns_by_accessor;
 
-    #    warn 'columns_by_accessor: ' . Dumper( \%columns_by_accessor ); use Data::Dumper;
     for my $name ( keys %$updates ) {
         my $source = $self->result_source;
         if ( $columns_by_accessor{$name}
     for my $name ( keys %$updates ) {
         my $source = $self->result_source;
         if ( $columns_by_accessor{$name}
@@ -55,12 +57,11 @@ sub recursive_update {
             $post_updates{$name} = $updates->{$name};
         }
     }
             $post_updates{$name} = $updates->{$name};
         }
     }
-
     # warn 'columns: ' . Dumper( \%columns ); use Data::Dumper;
 
     my $object;
     my @missing =
     # warn 'columns: ' . Dumper( \%columns ); use Data::Dumper;
 
     my $object;
     my @missing =
-      grep { !exists $columns{$_} && !exists $fixed_fields->{$_} } $self->result_source->primary_columns;
+      grep { !exists $columns{$_} && !exists $fixed_fields{$_} } $self->result_source->primary_columns;
     if ( !scalar @missing ) {
         $object = $self->find( \%columns, { key => 'primary' } );
     }
     if ( !scalar @missing ) {
         $object = $self->find( \%columns, { key => 'primary' } );
     }
@@ -85,7 +86,6 @@ sub recursive_update {
         next if exists $columns{$name};
         my $value = $updates->{$name};
 
         next if exists $columns{$name};
         my $value = $updates->{$name};
 
-        # many to many case
         if ( $self->is_m2m($name) ) {
             my ($pk) = $self->_get_pk_for_related($name);
             my @rows;
         if ( $self->is_m2m($name) ) {
             my ($pk) = $self->_get_pk_for_related($name);
             my @rows;
@@ -137,14 +137,14 @@ sub _update_relation {
         for my $sub_updates ( @{ $updates->{$name} } ) {
             $sub_updates = { %$sub_updates, %$resolved } if $resolved;
             my $sub_object =
         for my $sub_updates ( @{ $updates->{$name} } ) {
             $sub_updates = { %$sub_updates, %$resolved } if $resolved;
             my $sub_object =
-              $related_result->recursive_update( $sub_updates, $resolved );
+              $related_result->recursive_update( $sub_updates );
         }
     }
     else {
         my $sub_updates = $updates->{$name};
         $sub_updates = { %$sub_updates, %$resolved } if $resolved;
         my $sub_object =
         }
     }
     else {
         my $sub_updates = $updates->{$name};
         $sub_updates = { %$sub_updates, %$resolved } if $resolved;
         my $sub_object =
-          $related_result->recursive_update( $sub_updates, $resolved );
+          $related_result->recursive_update( $sub_updates );
         $object->set_from_related( $name, $sub_object );
     }
 }
         $object->set_from_related( $name, $sub_object );
     }
 }
@@ -290,8 +290,7 @@ like in the case of:
     
     my $restricted_rs = $user_rs->search( { id => 1 } );
 
     
     my $restricted_rs = $user_rs->search( { id => 1 } );
 
-then you need to specify that additional predicate as a second argument to the recursive_update
-method:
+then you need to inform recursive_update about additional predicate with a second argument:
 
     my $user = $restricted_rs->recursive_update( { 
         owned_dvds => [ 
 
     my $user = $restricted_rs->recursive_update( { 
         owned_dvds => [ 
@@ -300,7 +299,7 @@ method:
         } 
         ] 
       },
         } 
         ] 
       },
-      { id => 1 }
+      [ 'id' ]
     );
 
 
     );
 
 
index e8c601d1473bc1ff5e24e3fe2edb9a7b9dc513ae..1f31adacc9a5357728e399b8b4476ebe31230c40 100644 (file)
@@ -9,7 +9,7 @@ use Test::More;
 sub run_tests{
     my $schema = shift;
 
 sub run_tests{
     my $schema = shift;
 
-    plan tests => 27;
+    plan tests => 28;
     
     my $dvd_rs = $schema->resultset( 'Dvd' );
     my $user_rs = $schema->resultset( 'User' );
     
     my $dvd_rs = $schema->resultset( 'Dvd' );
     my $user_rs = $schema->resultset( 'User' );
@@ -79,7 +79,7 @@ sub run_tests{
 
 
 # changing existing records
 
 
 # changing existing records
-    
+
     my $num_of_users = $user_rs->count;
     $updates = {
             id => $dvd->id,
     my $num_of_users = $user_rs->count;
     $updates = {
             id => $dvd->id,
@@ -106,7 +106,12 @@ sub run_tests{
     is ( $dvd->tags->count, 0, 'Tags deleted' );
     is ( $dvd->liner_notes->notes, 'test note changed', 'might_have record changed' );
 
     is ( $dvd->tags->count, 0, 'Tags deleted' );
     is ( $dvd->liner_notes->notes, 'test note changed', 'might_have record changed' );
 
-
+    $updates = {
+            name => 'Test name 1',
+    };
+    $dvd = $dvd_rs->search( { id => $dvd->id } )->recursive_update( $updates, [ 'id' ] );
+    is ( $dvd->name, 'Test name 1', 'Dvd name set in a resultset with restricted id' );
     # repeatable
     
     $updates = {
     # repeatable
     
     $updates = {
index 14cf6784f1b014d8266bc6b547427e16d88d9dc0..b3a6f279a416c1a9fe650dd531557e7820253046 100644 (file)
Binary files a/t/var/dvdzbr.db and b/t/var/dvdzbr.db differ
This page took 0.023531 seconds and 4 git commands to generate.