From: zby Date: Tue, 12 May 2009 19:21:58 +0000 (+0000) Subject: m2m accepts one element instead of an arrayref; id as generic pk X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fp5-DBIx-Class-ResultSet-RecursiveUpdate;a=commitdiff_plain;h=34a44681170dc8a76ec09b1d655ed2c6d1d28e49 m2m accepts one element instead of an arrayref; id as generic pk --- diff --git a/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm b/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm index 0997ff3..f288135 100644 --- a/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm +++ b/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm @@ -45,6 +45,20 @@ sub recursive_update { my %columns_by_accessor = _get_columns_by_accessor( $self ); for my $name ( keys %$updates ) { my $source = $self->result_source; + if( $name eq 'id' +# && scalar @{$source->primary_columns} == 1 + && !$source->has_column( 'id' ) + ){ + my @ids = ( $updates->{id} ); + if( ref $updates->{id} ){ + @ids = @{ $updates->{id} }; + } + my $i = 0; + for my $key ( $source->primary_columns ){ + $columns{ $key } = $ids[ $i++ ]; + } + next; + } if ( $columns_by_accessor{$name} && !( $source->has_relationship($name) && ref( $updates->{$name} ) ) ) @@ -77,10 +91,9 @@ sub recursive_update { $object = $self->find( \%columns, { key => 'primary' } ); } $object ||= $self->new( {} ); - # first update columns and other accessors - so that later related records can be found for my $name ( keys %columns ) { - $object->$name( $updates->{$name} ); + $object->$name( $columns{$name} ); } for my $name ( keys %other_methods) { $object->$name( $updates->{$name} ) if $object->can( $name ); @@ -103,7 +116,14 @@ sub recursive_update { my ($pk) = _get_pk_for_related( $self, $name); my @rows; my $result_source = $object->$name->result_source; - for my $elem ( @{ $updates->{$name} } ) { + my @updates; + if( ref $updates->{$name} ){ + @updates = @{ $updates->{$name} }; + } + else{ + @updates = ( $updates->{$name} ); + } + for my $elem ( @updates ) { if ( ref $elem ) { push @rows, $result_source->resultset->find($elem); } diff --git a/t/lib/DBSchema/Result/Dvd.pm b/t/lib/DBSchema/Result/Dvd.pm index e791b76..0020109 100644 --- a/t/lib/DBSchema/Result/Dvd.pm +++ b/t/lib/DBSchema/Result/Dvd.pm @@ -8,11 +8,10 @@ use warnings; use base 'DBIx::Class'; use overload '""' => sub {$_[0]->name}, fallback => 1; -use lib '../../DBIx-Class-HTML-FormFu/lib/'; __PACKAGE__->load_components(qw/IntrospectableM2M Core/); __PACKAGE__->table('dvd'); __PACKAGE__->add_columns( - 'id' => { + 'dvd_id' => { data_type => 'integer', is_auto_increment => 1 }, @@ -45,11 +44,11 @@ __PACKAGE__->add_columns( is_nullable => 1, }, ); -__PACKAGE__->set_primary_key('id'); +__PACKAGE__->set_primary_key('dvd_id'); __PACKAGE__->belongs_to('owner', 'DBSchema::Result::User', { id => 'owner' }); __PACKAGE__->belongs_to('current_borrower', 'DBSchema::Result::User', { id => 'current_borrower' }); -__PACKAGE__->has_many('dvdtags', 'Dvdtag', { 'foreign.dvd' => 'self.id' }); -__PACKAGE__->has_many('viewings', 'DBSchema::Result::Viewing', { 'foreign.dvd_id' => 'self.id' }); +__PACKAGE__->has_many('dvdtags', 'Dvdtag', { 'foreign.dvd' => 'self.dvd_id' }); +__PACKAGE__->has_many('viewings', 'DBSchema::Result::Viewing', { 'foreign.dvd_id' => 'self.dvd_id' }); __PACKAGE__->many_to_many('tags', 'dvdtags' => 'tag'); __PACKAGE__->might_have( liner_notes => 'DBSchema::Result::LinerNotes', undef, diff --git a/t/lib/DBSchema/Result/Dvdtag.pm b/t/lib/DBSchema/Result/Dvdtag.pm index d17066a..68bdde9 100644 --- a/t/lib/DBSchema/Result/Dvdtag.pm +++ b/t/lib/DBSchema/Result/Dvdtag.pm @@ -14,7 +14,7 @@ __PACKAGE__->add_columns( "tag" => { data_type => 'integer' }, ); __PACKAGE__->set_primary_key("dvd", "tag"); -__PACKAGE__->belongs_to("dvd", "DBSchema::Result::Dvd", { id => "dvd" }); +__PACKAGE__->belongs_to("dvd", "DBSchema::Result::Dvd", { dvd_id => "dvd" }); __PACKAGE__->belongs_to("tag", "DBSchema::Result::Tag", { id => "tag" }); 1; diff --git a/t/lib/DBSchema/Result/Viewing.pm b/t/lib/DBSchema/Result/Viewing.pm index 13dcaa0..bf7e7ef 100755 --- a/t/lib/DBSchema/Result/Viewing.pm +++ b/t/lib/DBSchema/Result/Viewing.pm @@ -15,7 +15,7 @@ __PACKAGE__->belongs_to( __PACKAGE__->belongs_to( dvd => 'DBSchema::Result::Dvd', - {'foreign.id'=>'self.dvd_id'}, + {'foreign.dvd_id'=>'self.dvd_id'}, ); ; diff --git a/t/lib/RunTests.pm b/t/lib/RunTests.pm index fd4fe71..b5f447d 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 => 29; + plan tests => 30; my $dvd_rs = $schema->resultset( 'Dvd' ); my $user_rs = $schema->resultset( 'User' ); @@ -19,7 +19,7 @@ sub run_tests{ my $initial_user_count = $user_rs->count; my $initial_dvd_count = $dvd_rs->count; my $updates; - + # creating new record linked to some old record $updates = { name => 'Test name 2', @@ -81,7 +81,7 @@ sub run_tests{ my $num_of_users = $user_rs->count; $updates = { - id => $dvd->id, + id => $dvd->dvd_id, # id instead of dvd_id aaaa => undef, name => undef, tags => [ ], @@ -96,14 +96,15 @@ sub run_tests{ }, }; - $dvd = $dvd_rs->recursive_update( $updates ); - + my $dvd_updated = $dvd_rs->recursive_update( $updates ); + + is ( $dvd_updated->dvd_id, $dvd->dvd_id, 'Pk from "id"' ); is ( $schema->resultset( 'User' )->count, $initial_user_count + 1, "No new user created" ); - is ( $dvd->name, undef, 'Dvd name deleted' ); - is ( $dvd->owner->id, $another_owner->id, 'Owner updated' ); - is ( $dvd->current_borrower->name, 'new name a', 'Related record modified' ); - is ( $dvd->tags->count, 0, 'Tags deleted' ); - is ( $dvd->liner_notes->notes, 'test note changed', 'might_have record changed' ); + is ( $dvd_updated->name, undef, 'Dvd name deleted' ); + is ( $dvd_updated->owner->id, $another_owner->id, 'Owner updated' ); + is ( $dvd_updated->current_borrower->name, 'new name a', 'Related record modified' ); + is ( $dvd_updated->tags->count, 0, 'Tags deleted' ); + is ( $dvd_updated->liner_notes->notes, 'test note changed', 'might_have record changed' ); # repeatable diff --git a/t/var/dvdzbr.db b/t/var/dvdzbr.db index c31073d..f96d3d5 100644 Binary files a/t/var/dvdzbr.db and b/t/var/dvdzbr.db differ