From: Zbigniew Lukasiak Date: Thu, 11 Jun 2009 07:19:29 +0000 (+0200) Subject: if_not_submitted X-Git-Url: https://git.dogcows.com/gitweb?a=commitdiff_plain;h=df9cba8acd38f0929da6d62ec6980cbc3415421a;p=chaz%2Fp5-DBIx-Class-ResultSet-RecursiveUpdate if_not_submitted --- diff --git a/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm b/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm index fcbad3b..90f5994 100644 --- a/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm +++ b/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm @@ -22,7 +22,7 @@ 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/ }; + my ( $self, $updates, $fixed_fields, $object, $resolved, $if_not_submitted ) = @params{ qw/resultset updates fixed_fields object resolved if_not_submitted/ }; $resolved ||= {}; # warn 'entering: ' . $self->result_source->from(); carp 'fixed fields needs to be an array ref' if $fixed_fields && ref($fixed_fields) ne 'ARRAY'; @@ -37,12 +37,15 @@ sub recursive_update { my @missing = grep { !exists $updates->{$_} && !exists $fixed_fields{$_} } $self->result_source->primary_columns; if ( !$object && !scalar @missing ) { +# warn 'finding by: ' . Dumper( $updates ); use Data::Dumper; $object = $self->find( $updates, { key => 'primary' } ); } + $updates = { %$updates, %$resolved }; @missing = grep { !exists $resolved->{$_} } @missing; if ( !$object && !scalar @missing ) { - $object = $self->find( \%{ %$updates, %$resolved }, { key => 'primary' } ); +# warn 'finding by +resolved: ' . Dumper( $updates ); use Data::Dumper; + $object = $self->find( $updates, { key => 'primary' } ); } $object ||= $self->new( {} ); # warn Dumper( $updates ); use Data::Dumper; @@ -59,7 +62,6 @@ sub recursive_update { 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 ) { @@ -99,7 +101,7 @@ sub recursive_update { } for my $name ( keys %pre_updates ) { my $info = $object->result_source->relationship_info($name); - _update_relation( $self, $name, $updates, $object, $info ); + _update_relation( $self, $name, $updates, $object, $info, $if_not_submitted ); } # $self->_delete_empty_auto_increment($object); # don't allow insert to recurse to related objects - we do the recursion ourselves @@ -142,7 +144,7 @@ sub recursive_update { } for my $name ( keys %post_updates ) { my $info = $object->result_source->relationship_info($name); - _update_relation( $self, $name, $updates, $object, $info ); + _update_relation( $self, $name, $updates, $object, $info, $if_not_submitted ); } return $object; } @@ -160,7 +162,7 @@ sub _get_columns_by_accessor { } sub _update_relation { - my ( $self, $name, $updates, $object, $info ) = @_; + my ( $self, $name, $updates, $object, $info, $if_not_submitted ) = @_; my $related_result = $self->related_resultset($name)->result_source->resultset; my $resolved; @@ -175,9 +177,23 @@ sub _update_relation { $resolved = {} if defined $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION && $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION == $resolved; if ( ref $updates->{$name} eq 'ARRAY' ) { + my @updated_ids; for my $sub_updates ( @{ $updates->{$name} } ) { my $sub_object = recursive_update( resultset => $related_result, updates => $sub_updates, resolved => $resolved ); + push @updated_ids, $sub_object->id; + } + my @related_pks = $related_result->result_source->primary_columns; + if( defined $if_not_submitted && $if_not_submitted eq 'delete' ){ + if ( 1 == scalar @related_pks ){ + $object->$name->search( { $related_pks[0] => { -not_in => \@updated_ids } } )->delete; + } + } + elsif( defined $if_not_submitted && $if_not_submitted eq 'set_to_null' ){ + if ( 1 == scalar @related_pks ){ + my @fk = keys %$resolved; + $object->$name->search( { $related_pks[0] => { -not_in => \@updated_ids } } )->update( { $fk[0] => undef } ); + } } } else { diff --git a/t/lib/RunTests.pm b/t/lib/RunTests.pm index 93255b6..8e5afa8 100644 --- a/t/lib/RunTests.pm +++ b/t/lib/RunTests.pm @@ -9,7 +9,7 @@ use DBIx::Class::ResultSet::RecursiveUpdate; sub run_tests{ my $schema = shift; - plan tests => 30; + plan tests => 36; my $dvd_rs = $schema->resultset( 'Dvd' ); my $user_rs = $schema->resultset( 'User' ); @@ -165,6 +165,40 @@ sub run_tests{ $dvd = $dvd_rs->find( 1 ); is( $dvd->get_column( 'owner' ), $user->id, 'foreign key set' ); + $dvd_rs->update( { current_borrower => $user->id } ); + ok( $user->borrowed_dvds->count > 1, 'Precond' ); + $updates = { + id => $user->id, + borrowed_dvds =>[ + { + id => $dvd->id + }, + ] + }; + $user = DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update( + resultset => $user_rs, + updates => $updates, + if_not_submitted => 'set_to_null', + ); + is( $user->borrowed_dvds->count, 1, 'set_to_null' ); + + $dvd_rs->update( { current_borrower => $user->id } ); + $updates = { + id => $user->id, + borrowed_dvds =>[ + { + id => $dvd->id + }, + ] + }; + $user = DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update( + resultset => $user_rs, + updates => $updates, + if_not_submitted => 'delete', + ); + is( $user->borrowed_dvds->count, 1, 'if_not_submitted delete' ); + + # $updates = { # name => 'Test name 1', # }; diff --git a/t/var/dvdzbr.db b/t/var/dvdzbr.db index 360cb60..08a30ef 100644 Binary files a/t/var/dvdzbr.db and b/t/var/dvdzbr.db differ