]> Dogcows Code - chaz/p5-DBIx-Class-ResultSet-RecursiveUpdate/commitdiff
if_not_submitted
authorZbigniew Lukasiak <zby@zby.aster.net.pl>
Thu, 11 Jun 2009 07:19:29 +0000 (09:19 +0200)
committerZbigniew Lukasiak <zby@zby.aster.net.pl>
Thu, 11 Jun 2009 07:19:29 +0000 (09:19 +0200)
lib/DBIx/Class/ResultSet/RecursiveUpdate.pm
t/lib/RunTests.pm
t/var/dvdzbr.db

index fcbad3bbf816ea9fe858d1b83d3343dbaa2079dd..90f5994d5d419a0b46b9dd0cc5a7a3cc32701e49 100644 (file)
@@ -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 {
index 93255b653c6e9e7fcfdffbde61377a7e8b0a01cc..8e5afa8e3bc0e83b01aa125ea8497345140cceff 100644 (file)
@@ -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',
 #    };
index 360cb608b934b43c7c0871489874de929a51af2d..08a30ef83cc1b6f61e6d85dbebafd87da9af68b5 100644 (file)
Binary files a/t/var/dvdzbr.db and b/t/var/dvdzbr.db differ
This page took 0.026791 seconds and 4 git commands to generate.