+
+ 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};
+ }
+ 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 } );
+ }
+ }
+ my $set_meth = 'set_' . $name;
+ $object->$set_meth( \@rows );
+ }
+ }
+ for my $name ( keys %post_updates ) {
+
+ #warn "post_update $name\n";
+ _update_relation( $self, $name, $post_updates{$name}, $object,
+ $if_not_submitted );
+ }
+ return $object;
+}
+
+# returns DBIx::Class::ResultSource::column_info as a hash indexed by column accessor || name
+sub _get_columns_by_accessor {
+ my $self = shift;
+ my $source = $self->result_source;
+ my %columns;
+ for my $name ( $source->columns ) {
+ my $info = $source->column_info($name);
+ $info->{name} = $name;
+ $columns{ $info->{accessor} || $name } = $info;
+ }
+ return %columns;
+}
+
+# Arguments: $rs, $name, $updates, $row, $if_not_submitted
+sub _update_relation {
+ my ( $self, $name, $updates, $object, $if_not_submitted ) = @_;
+
+ # this should never happen because we're checking the paramters passed to
+ # recursive_update, but just to be sure...
+ $object->throw_exception("No such relationship '$name'")
+ unless $object->has_relationship($name);
+
+ #warn "_update_relation $name: OBJ: " . ref($object) . "\n";
+
+ my $info = $object->result_source->relationship_info($name);
+
+ # get a related resultset without a condition
+ my $related_resultset =
+ $self->related_resultset($name)->result_source->resultset;
+ my $resolved;
+ if ( $self->result_source->can('_resolve_condition') ) {
+ $resolved =
+ $self->result_source->_resolve_condition( $info->{cond}, $name,
+ $object );
+ }
+ else {
+ $self->throw_exception(
+ "result_source must support _resolve_condition");
+ }
+
+ # warn "$name resolved: " . Dumper( $resolved ); use Data::Dumper;
+ $resolved = {}
+ if defined $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION
+ && $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION
+ == $resolved;
+
+ my @rel_cols = keys %{ $info->{cond} };
+ map {s/^foreign\.//} @rel_cols;
+
+ #warn "REL_COLS: " . Dumper(@rel_cols); use Data::Dumper;
+ #my $rel_col_cnt = scalar @rel_cols;
+
+ # find out if all related columns are nullable
+ my $all_fks_nullable = 1;
+ for my $rel_col (@rel_cols) {
+ $all_fks_nullable = 0
+ unless $related_resultset->result_source->column_info($rel_col)
+ ->{is_nullable};
+ }
+
+ $if_not_submitted = $all_fks_nullable ? 'nullify' : 'delete'
+ unless defined $if_not_submitted;
+
+ #warn "\tNULLABLE: $all_fks_nullable ACTION: $if_not_submitted\n";
+
+ #warn "RELINFO for $name: " . Dumper($info); use Data::Dumper;
+
+ # the only valid datatype for a has_many rels is an arrayref
+ if ( $info->{attrs}{accessor} eq 'multi' ) {
+
+ # handle undef like empty arrayref
+ $updates = []
+ unless defined $updates;
+ $self->throw_exception(
+ "data for has_many relationship '$name' must be an arrayref")
+ unless ref $updates eq 'ARRAY';
+
+ my @updated_objs;
+
+ #warn "\tupdating has_many rel '$name' ($rel_col_cnt columns cols)\n";
+ for my $sub_updates ( @{$updates} ) {
+ my $sub_object = recursive_update(
+ resultset => $related_resultset,
+ updates => $sub_updates,
+ resolved => $resolved
+ );
+
+ push @updated_objs, $sub_object;
+ }
+
+ #warn "\tcreated and updated related rows\n";
+
+ my @related_pks = $related_resultset->result_source->primary_columns;
+
+ my $rs_rel_delist = $object->$name;
+
+ # foreign table has a single pk column
+ if ( scalar @related_pks == 1 ) {
+ $rs_rel_delist = $rs_rel_delist->search_rs(
+ { $related_pks[0] =>
+ { -not_in => [ map ( $_->id, @updated_objs ) ] }