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';
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;
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 ) {
}
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
}
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;
}
}
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;
$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 {
sub run_tests{
my $schema = shift;
- plan tests => 30;
+ plan tests => 36;
my $dvd_rs = $schema->resultset( 'Dvd' );
my $user_rs = $schema->resultset( 'User' );
$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',
# };