From df9cba8acd38f0929da6d62ec6980cbc3415421a Mon Sep 17 00:00:00 2001 From: Zbigniew Lukasiak Date: Thu, 11 Jun 2009 09:19:29 +0200 Subject: [PATCH 1/1] if_not_submitted --- lib/DBIx/Class/ResultSet/RecursiveUpdate.pm | 28 +++++++++++---- t/lib/RunTests.pm | 36 +++++++++++++++++++- t/var/dvdzbr.db | Bin 37888 -> 37888 bytes 3 files changed, 57 insertions(+), 7 deletions(-) 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 360cb608b934b43c7c0871489874de929a51af2d..08a30ef83cc1b6f61e6d85dbebafd87da9af68b5 100644 GIT binary patch delta 1112 zcmah|O>7fa5Z;~HXMzdAjh)0KkufBNB*c-uhVUaogCJ4>)u?GiAxR5{Y!cgX{%iyJ zK_W;M$^l52!yMs)gg{6TOFi_^P)rpZ7?gZ`AsyX{ITwSyP9__S#nVoq(@|CA>~WGAVcLyK#+qoW*V`{i zb3>6a>!c@wV^TOK1x-{e2uaXZ`h@*XJyNLGZYk1#?xZs5L?#)R9qp)S#T#l>b~qdj zg(IOzd(e*VkJ$Ue5jXy7nE11WtN43qo5419&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