-sub recursive_update {
- my( $self, $updates ) = @_;
- my $object;
- $object = $self->find( $updates, { key => 'primary' } ) || $self->new( {} );
-
- for my $name ( keys %$updates ){
- if($object->can($name)){
- my $value = $updates->{$name};
-
- # updating relations that that should be done before the row is inserted into the database
- # like belongs_to
- if( $object->result_source->has_relationship($name)
- and
- ref $value
- ){
- my $info = $object->result_source->relationship_info( $name );
- if( $info and not $info->{attrs}{accessor} eq 'multi'
- and
- _master_relation_cond( $object, $info->{cond}, $self->_get_pk_for_related( $name ) )
- ){
- my $related_result = $object->related_resultset( $name );
- my $sub_object = $related_result->recursive_update( $value );
- $object->set_from_related( $name, $sub_object );
- }
+ # warn 'finding by +resolved: ' . Dumper( $updates ); use Data::Dumper;
+ $object = $self->find( $updates, { 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, might_have and has_one
+ my %post_updates;
+ my %other_methods;
+ my %columns_by_accessor = _get_columns_by_accessor($self);
+
+ # warn 'resolved: ' . Dumper( $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;
+
+ # first update columns and other accessors
+ # so that later related records can be found
+ for my $name ( keys %columns ) {
+ $object->$name( $columns{$name} );
+ }
+ for my $name ( keys %other_methods ) {
+ $object->$name( $updates->{$name} ) if $object->can($name);
+ }
+ for my $name ( keys %pre_updates ) {
+ my $info = $object->result_source->relationship_info($name);
+ _update_relation( $self, $name, $updates->{$name}, $object, $info,
+ $if_not_submitted );
+ }
+
+ # $self->_delete_empty_auto_increment($object);
+ # don't allow insert to recurse to related objects
+ # do the recursion ourselves
+ # $object->{_rel_in_storage} = 1;
+ $object->update_or_insert if $object->is_changed;
+
+ # updating many_to_many
+ for my $name ( keys %$updates ) {
+ next if exists $columns{$name};
+ my $value = $updates->{$name};
+
+ if ( is_m2m( $self, $name ) ) {
+ 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};