From: zby Date: Mon, 16 Feb 2009 12:45:27 +0000 (+0000) Subject: fixed_fields as array X-Git-Url: https://git.brokenzipper.com/gitweb?a=commitdiff_plain;h=073ab86177b3de3f95b1d2a0dc97f3e8e582c7a6;p=chaz%2Fp5-DBIx-Class-ResultSet-RecursiveUpdate fixed_fields as array --- 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 14cf678..b3a6f27 100644 Binary files a/t/var/dvdzbr.db and b/t/var/dvdzbr.db differ