From 073ab86177b3de3f95b1d2a0dc97f3e8e582c7a6 Mon Sep 17 00:00:00 2001 From: zby Date: Mon, 16 Feb 2009 12:45:27 +0000 Subject: [PATCH] fixed_fields as array --- lib/DBIx/Class/ResultSet/RecursiveUpdate.pm | 21 ++++++++++---------- t/lib/RunTests.pm | 11 +++++++--- t/var/dvdzbr.db | Bin 37888 -> 37888 bytes 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm b/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm index 59b2a64..1c40825 100644 --- a/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm +++ b/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm @@ -11,13 +11,16 @@ use base qw(DBIx::Class::ResultSet); sub recursive_update { my ( $self, $updates, $fixed_fields ) = @_; - # warn 'entering: ' . $self->result_source->from(); + + carp 'fixed fields needs to be an array ref' if $fixed_fields && ref($fixed_fields) ne 'ARRAY'; + my %fixed_fields; + %fixed_fields = map { $_ => 1 } @$fixed_fields if $fixed_fields; + if ( blessed($updates) && $updates->isa('DBIx::Class::Row') ) { return $updates; } - carp 'fixed fields needs to be a hash ref' if $fixed_fields && ref($fixed_fields) ne 'HASH'; # direct column accessors my %columns; @@ -31,7 +34,6 @@ sub recursive_update { my %post_updates; my %columns_by_accessor = $self->_get_columns_by_accessor; - # warn 'columns_by_accessor: ' . Dumper( \%columns_by_accessor ); use Data::Dumper; for my $name ( keys %$updates ) { my $source = $self->result_source; if ( $columns_by_accessor{$name} @@ -55,12 +57,11 @@ sub recursive_update { $post_updates{$name} = $updates->{$name}; } } - # warn 'columns: ' . Dumper( \%columns ); use Data::Dumper; my $object; my @missing = - grep { !exists $columns{$_} && !exists $fixed_fields->{$_} } $self->result_source->primary_columns; + grep { !exists $columns{$_} && !exists $fixed_fields{$_} } $self->result_source->primary_columns; if ( !scalar @missing ) { $object = $self->find( \%columns, { key => 'primary' } ); } @@ -85,7 +86,6 @@ sub recursive_update { next if exists $columns{$name}; my $value = $updates->{$name}; - # many to many case if ( $self->is_m2m($name) ) { my ($pk) = $self->_get_pk_for_related($name); my @rows; @@ -137,14 +137,14 @@ sub _update_relation { for my $sub_updates ( @{ $updates->{$name} } ) { $sub_updates = { %$sub_updates, %$resolved } if $resolved; my $sub_object = - $related_result->recursive_update( $sub_updates, $resolved ); + $related_result->recursive_update( $sub_updates ); } } else { my $sub_updates = $updates->{$name}; $sub_updates = { %$sub_updates, %$resolved } if $resolved; my $sub_object = - $related_result->recursive_update( $sub_updates, $resolved ); + $related_result->recursive_update( $sub_updates ); $object->set_from_related( $name, $sub_object ); } } @@ -290,8 +290,7 @@ like in the case of: my $restricted_rs = $user_rs->search( { id => 1 } ); -then you need to specify that additional predicate as a second argument to the recursive_update -method: +then you need to inform recursive_update about additional predicate with a second argument: my $user = $restricted_rs->recursive_update( { owned_dvds => [ @@ -300,7 +299,7 @@ method: } ] }, - { id => 1 } + [ 'id' ] ); diff --git a/t/lib/RunTests.pm b/t/lib/RunTests.pm index e8c601d..1f31ada 100644 --- a/t/lib/RunTests.pm +++ b/t/lib/RunTests.pm @@ -9,7 +9,7 @@ use Test::More; sub run_tests{ my $schema = shift; - plan tests => 27; + plan tests => 28; my $dvd_rs = $schema->resultset( 'Dvd' ); my $user_rs = $schema->resultset( 'User' ); @@ -79,7 +79,7 @@ sub run_tests{ # changing existing records - + my $num_of_users = $user_rs->count; $updates = { id => $dvd->id, @@ -106,7 +106,12 @@ sub run_tests{ is ( $dvd->tags->count, 0, 'Tags deleted' ); is ( $dvd->liner_notes->notes, 'test note changed', 'might_have record changed' ); - + $updates = { + name => 'Test name 1', + }; + $dvd = $dvd_rs->search( { id => $dvd->id } )->recursive_update( $updates, [ 'id' ] ); + is ( $dvd->name, 'Test name 1', 'Dvd name set in a resultset with restricted id' ); + # repeatable $updates = { diff --git a/t/var/dvdzbr.db b/t/var/dvdzbr.db index 14cf6784f1b014d8266bc6b547427e16d88d9dc0..b3a6f279a416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