]> Dogcows Code - chaz/p5-DBIx-Class-ResultSet-RecursiveUpdate/blobdiff - lib/DBIx/Class/ResultSet/RecursiveUpdate.pm
experimental
[chaz/p5-DBIx-Class-ResultSet-RecursiveUpdate] / lib / DBIx / Class / ResultSet / RecursiveUpdate.pm
index 59b2a6405a0db607b244454fe806b1b0284a4820..7275d1ef63844d5dad79d538e19b78a525e9142c 100644 (file)
@@ -11,13 +11,16 @@ use base qw(DBIx::Class::ResultSet);
 
 sub recursive_update {
     my ( $self, $updates, $fixed_fields ) = @_;
-
     # 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;
     }
 
-    carp 'fixed fields needs to be a hash ref' if $fixed_fields && ref($fixed_fields) ne 'HASH';
 
     # direct column accessors
     my %columns;
@@ -31,7 +34,6 @@ sub recursive_update {
     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}
@@ -55,12 +57,11 @@ sub recursive_update {
             $post_updates{$name} = $updates->{$name};
         }
     }
-
     # 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' } );
     }
@@ -74,7 +75,7 @@ sub recursive_update {
         my $info = $object->result_source->relationship_info($name);
         $self->_update_relation( $name, $updates, $object, $info );
     }
-    $self->_delete_empty_auto_increment($object);
+#    $self->_delete_empty_auto_increment($object);
 
 # don't allow insert to recurse to related objects - we do the recursion ourselves
 #    $object->{_rel_in_storage} = 1;
@@ -85,7 +86,6 @@ sub recursive_update {
         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;
@@ -132,19 +132,19 @@ sub _update_relation {
 
  #                    warn 'resolved: ' . Dumper( $resolved ); use Data::Dumper;
     $resolved = undef
-      if $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION == $resolved;
+      if defined $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION && $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION == $resolved;
     if ( ref $updates->{$name} eq 'ARRAY' ) {
         for my $sub_updates ( @{ $updates->{$name} } ) {
-            $sub_updates = { %$sub_updates, %$resolved } if $resolved;
+            $sub_updates = { %$sub_updates, %$resolved } if $resolved && ref( $sub_updates ) eq 'HASH';
             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;
+        $sub_updates = { %$sub_updates, %$resolved } if $resolved && ref( $sub_updates ) eq 'HASH';
         my $sub_object =
-          $related_result->recursive_update( $sub_updates, $resolved );
+          $related_result->recursive_update( $sub_updates );
         $object->set_from_related( $name, $sub_object );
     }
 }
@@ -270,6 +270,7 @@ Then:
 
   
 =head1 DESCRIPTION
+This is the first release - so treat it as experimental.
 
 You can feed the ->create method with a recursive datastructure and have the related records
 created.  Unfortunately you cannot do a similar thing with update_or_create - this module
@@ -290,8 +291,7 @@ like in the case of:
     
     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 => [ 
@@ -300,15 +300,16 @@ method:
         } 
         ] 
       },
-      { id => 1 }
+      [ 'id' ]
     );
 
+This will work with a new DBIC release.
 
 For a many_to_many (pseudo) relation you can supply a list of primary keys
 from the other table - and it will link the record at hand to those and
 only those records identified by them.  This is convenient for handling web
 forms with check boxes (or a SELECT box with multiple choice) that let you
-update such (pseudo) relations.
+update such (pseudo) relations.  
 
 For a description how to set up base classes for ResultSets see load_namespaces
 in DBIx::Class::Schema.
This page took 0.02025 seconds and 4 git commands to generate.