X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fp5-DBIx-Class-ResultSet-RecursiveUpdate;a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FResultSet%2FRecursiveUpdate.pm;h=f8b567de3a2f6c701f5999a7ec1c87f08e301df8;hp=2d0c235ae5370a532b3add0ce8401ac6c0f70b39;hb=HEAD;hpb=d94f7e813dcc0fa5de6d46a0c9739fdba22b44cf diff --git a/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm b/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm index 2d0c235..f8b567d 100644 --- a/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm +++ b/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm @@ -37,6 +37,7 @@ package DBIx::Class::ResultSet::RecursiveUpdate::Functions; use Carp::Clan qw/^DBIx::Class|^HTML::FormHandler|^Try::Tiny/; use Scalar::Util qw( blessed ); use List::MoreUtils qw/ any /; +use Try::Tiny; sub recursive_update { my %params = @_; @@ -47,36 +48,67 @@ sub recursive_update { }; $resolved ||= {}; - # warn 'entering: ' . $self->result_source->from(); - carp 'fixed fields needs to be an array ref' + my $source = $self->result_source; + + croak "first parameter needs to be defined" + unless defined $updates; + + croak "first parameter needs to be a hashref" + unless ref($updates) eq 'HASH'; + + # warn 'entering: ' . $source->from(); + croak 'fixed fields needs to be an arrayref' if defined $fixed_fields && ref $fixed_fields ne 'ARRAY'; + # always warn about additional parameters if storage debugging is enabled + $unknown_params_ok = 0 + if $source->storage->debug; + if ( blessed($updates) && $updates->isa('DBIx::Class::Row') ) { return $updates; } - if ( $updates->{id} ) { + + if ( !defined $object && exists $updates->{id} ) { + + # warn "finding object by id " . $updates->{id} . "\n"; $object = $self->find( $updates->{id}, { key => 'primary' } ); + + # warn "object not found by id\n" + # unless defined $object; } my %fixed_fields = map { $_ => 1 } @$fixed_fields if $fixed_fields; + + # the updates hashref might contain the pk columns + # but with an undefined value my @missing = - grep { !exists $updates->{$_} && !exists $fixed_fields{$_} } - $self->result_source->primary_columns; - if ( !$object && !scalar @missing ) { + grep { !defined $updates->{$_} && !exists $fixed_fields{$_} } + $source->primary_columns; + + # warn "MISSING: " . join(', ', @missing) . "\n"; + if ( !defined $object && scalar @missing == 0 ) { # warn 'finding by: ' . Dumper( $updates ); use Data::Dumper; $object = $self->find( $updates, { key => 'primary' } ); } + + # add the resolved columns to the updates hashref $updates = { %$updates, %$resolved }; - @missing = - grep { !exists $resolved->{$_} } @missing; - if ( !$object && !scalar @missing ) { + + # the resolved hashref might contain the pk columns + # but with an undefined value + @missing = grep { !defined $resolved->{$_} } @missing; + + #warn "MISSING2: " . join( ', ', @missing ) . "\n"; + if ( !defined $object && scalar @missing == 0 ) { # warn 'finding by +resolved: ' . Dumper( $updates ); use Data::Dumper; $object = $self->find( $updates, { key => 'primary' } ); } - $object ||= $self->new( {} ); + + $object = $self->new_result( {} ) + unless defined $object; # warn Dumper( $updates ); use Data::Dumper; # direct column accessors @@ -90,13 +122,13 @@ sub recursive_update { # database like has_many, might_have and has_one my %post_updates; my %other_methods; + my %m2m_accessors; 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; # columns if ( exists $columns_by_accessor{$name} @@ -129,7 +161,7 @@ sub recursive_update { if ( is_m2m( $self, $name ) ) { #warn "$name is a many-to-many helper accessor\n"; - $other_methods{$name} = $updates->{$name}; + $m2m_accessors{$name} = $updates->{$name}; next; } @@ -166,7 +198,7 @@ sub recursive_update { for my $name ( keys %other_methods ) { #warn "update other $name\n"; - $object->$name( $updates->{$name} ); + $object->$name( $other_methods{$name} ); } for my $name ( keys %pre_updates ) { @@ -175,52 +207,53 @@ sub recursive_update { $if_not_submitted ); } - # $self->_delete_empty_auto_increment($object); + _delete_empty_auto_increment($object); # don't allow insert to recurse to related objects # do the recursion ourselves # $object->{_rel_in_storage} = 1; - #warn "CHANGED: " . $object->is_changed . "\n": + #warn "CHANGED: " . $object->is_changed . "\n"; #warn "IN STOR: " . $object->in_storage . "\n"; $object->update_or_insert if $object->is_changed; $object->discard_changes; # updating many_to_many - for my $name ( keys %$updates ) { - next if exists $columns{$name}; - my $value = $updates->{$name}; - - 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; + for my $name ( keys %m2m_accessors ) { + my $value = $m2m_accessors{$name}; + + #warn "update m2m $name\n"; + # TODO: only first pk col is used + my ($pk) = _get_pk_for_related( $self, $name ); + my @rows; + my $result_source = $object->$name->result_source; + my @updates; + if ( defined $value && ref $value eq 'ARRAY' ) { + @updates = @{$value}; + } + elsif ( defined $value && !ref $value ) { + @updates = ($value); + } + elsif ( defined $value ) { + carp + "value of many-to-many rel '$name' must be an arrayref or scalar: $value"; + } + for my $elem (@updates) { + if ( blessed($elem) && $elem->isa('DBIx::Class::Row') ) { + push @rows, $elem; } - elsif ( ref $value ) { - @updates = @{$value}; + elsif ( ref $elem eq 'HASH' ) { + push @rows, + recursive_update( + resultset => $result_source->resultset, + updates => $elem + ); } else { - @updates = ($value); + push @rows, + $result_source->resultset->find( { $pk => $elem } ); } - 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 ); } + my $set_meth = 'set_' . $name; + $object->$set_meth( \@rows ); } for my $name ( keys %post_updates ) { @@ -277,6 +310,8 @@ sub _update_relation { && $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION == $resolved; + #warn "RESOLVED: " . Dumper($resolved); use Data::Dumper; + my @rel_cols = keys %{ $info->{cond} }; map {s/^foreign\.//} @rel_cols; @@ -291,7 +326,7 @@ sub _update_relation { ->{is_nullable}; } - $if_not_submitted = $all_fks_nullable ? 'nullify' : 'delete' + $if_not_submitted = $all_fks_nullable ? 'set_to_null' : 'delete' unless defined $if_not_submitted; #warn "\tNULLABLE: $all_fks_nullable ACTION: $if_not_submitted\n"; @@ -352,9 +387,9 @@ sub _update_relation { $rs_rel_delist = $rs_rel_delist->search_rs( { -not => [@cond] } ); } + #warn "\tCOND: " . Dumper(\@cond); } - #warn "\tCOND: " . Dumper(\%cond); #my $rel_delist_cnt = $rs_rel_delist->count; if ( $if_not_submitted eq 'delete' ) { @@ -375,9 +410,12 @@ sub _update_relation { #warn "\tupdating rel '$name': $if_not_submitted\n"; my $sub_object; if ( ref $updates ) { + if ( blessed($updates) && $updates->isa('DBIx::Class::Row') ) { + $sub_object = $updates; + } # for might_have relationship - if ( $info->{attrs}{accessor} eq 'single' + elsif ( $info->{attrs}{accessor} eq 'single' && defined $object->$name ) { $sub_object = recursive_update( @@ -425,7 +463,7 @@ sub is_m2m { if ( $rclass->can('_m2m_metadata') ) { return $rclass->_m2m_metadata->{$relation}; } - my $object = $self->new( {} ); + my $object = $self->new_result( {} ); if ( $object->can($relation) and !$self->result_source->has_relationship($relation) and $object->can( 'set_' . $relation ) ) @@ -446,13 +484,13 @@ sub get_m2m_source { ->related_source( $rclass->_m2m_metadata->{$relation}{foreign_relation} ); } - my $object = $self->new( {} ); + my $object = $self->new_result( {} ); my $r = $object->$relation; return $r->result_source; } sub _delete_empty_auto_increment { - my ( $self, $object ) = @_; + my ( $object ) = @_; for my $col ( keys %{ $object->{_column_data} } ) { if ($object->result_source->column_info($col)->{is_auto_increment} and ( !defined $object->{_column_data}{$col}