+use Scalar::Util qw( blessed );
+
+
+sub recursive_update {
+ my %params = @_;
+ my ( $self, $updates, $fixed_fields, $object, $resolved ) = @params{ qw/resultset updates fixed_fields object resolved/ };
+ $resolved ||= {};
+ # 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 ( $updates->{id} ){
+ $object = $self->find( $updates->{id}, { key => 'primary' } );
+ }
+ my @missing =
+ grep { !exists $updates->{$_} && !exists $fixed_fields{$_} } $self->result_source->primary_columns;
+ if ( !$object && !scalar @missing ) {
+ $object = $self->find( $updates, { key => 'primary' } );
+ }
+ @missing =
+ grep { !exists $resolved->{$_} } @missing;
+ if ( !$object && !scalar @missing ) {
+ $object = $self->find( \%{ %$updates, %$resolved }, { key => 'primary' } );
+ }
+ $object ||= $self->new( {} );
+ # warn Dumper( $updates ); use Data::Dumper;
+ # direct column accessors
+ my %columns;
+
+ # relations that that should be done before the row is inserted into the database
+ # like belongs_to
+ my %pre_updates;
+
+ # relations that that should be done after the row is inserted into the database
+ # like has_many and might_have
+ my %post_updates;
+ my %other_methods;
+ my %columns_by_accessor = _get_columns_by_accessor( $self );
+# warn 'resolved: ' . Dumper( $resolved );
+ $updates = { %$updates, %$resolved };
+# warn 'updates: ' . Dumper( $updates ); use Data::Dumper;
+# warn 'columns: ' . Dumper( \%columns_by_accessor );
+ for my $name ( keys %$updates ) {
+ my $source = $self->result_source;
+ if ( $columns_by_accessor{$name}
+ && !( $source->has_relationship($name) && ref( $updates->{$name} ) )
+ )
+ {
+ $columns{$name} = $updates->{$name};
+ next;
+ }
+ if( !( $source->has_relationship($name) ) ){
+ $other_methods{$name} = $updates->{$name};
+ next;
+ }
+ my $info = $source->relationship_info($name);
+ if (
+ _master_relation_cond(
+ $source, $info->{cond}, _get_pk_for_related( $self, $name)
+ )
+ )
+ {
+ $pre_updates{$name} = $updates->{$name};
+ }
+ else {
+ $post_updates{$name} = $updates->{$name};
+ }
+ }
+ # warn 'other: ' . Dumper( \%other_methods ); use Data::Dumper;