From: zby Date: Mon, 4 May 2009 22:30:59 +0000 (+0000) Subject: might_have with non pk fk X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fp5-DBIx-Class-ResultSet-RecursiveUpdate;a=commitdiff_plain;h=f6641f7ade5a9b4227feef8a538228fcea80a5b2 might_have with non pk fk --- diff --git a/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm b/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm index ca6a40d..884cf05 100644 --- a/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm +++ b/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm @@ -30,8 +30,6 @@ sub recursive_update { if ( blessed($updates) && $updates->isa('DBIx::Class::Row') ) { return $updates; } - - # direct column accessors my %columns; @@ -76,7 +74,7 @@ sub recursive_update { } $object ||= $self->new( {} ); -# first update columns and other accessors - so that later related records can be found + # first update columns and other accessors - so that later related records can be found for my $name ( keys %columns ) { $object->$name( $updates->{$name} ); } @@ -85,7 +83,6 @@ sub recursive_update { _update_relation( $self, $name, $updates, $object, $info ); } # $self->_delete_empty_auto_increment($object); - # don't allow insert to recurse to related objects - we do the recursion ourselves # $object->{_rel_in_storage} = 1; $object->update_or_insert; @@ -151,8 +148,18 @@ sub _update_relation { else { my $sub_updates = $updates->{$name}; $sub_updates = { %$sub_updates, %$resolved } if $resolved && ref( $sub_updates ) eq 'HASH'; - my $sub_object = - recursive_update( resultset => $related_result, updates => $sub_updates ); + my $sub_object; + if( $info->{attrs}{accessor} eq 'single' && defined $object->$name ){ + $sub_object = recursive_update( + resultset => $related_result, + updates => $sub_updates, + object => $object->$name + ); + } + else{ + $sub_object = + recursive_update( resultset => $related_result, updates => $sub_updates ); + } $object->set_from_related( $name, $sub_object ); } } diff --git a/t/lib/DBSchema.pm b/t/lib/DBSchema.pm index f44ee45..e729155 100644 --- a/t/lib/DBSchema.pm +++ b/t/lib/DBSchema.pm @@ -15,7 +15,7 @@ sub get_test_schema { $dsn ||= 'dbi:SQLite:dbname=t/var/dvdzbr.db'; warn "testing $dsn"; my $schema = __PACKAGE__->connect( $dsn, $user, $pass, {} ); - $schema->deploy({ }); + $schema->deploy({ add_drop_table => 1, }); $schema->populate('User', [ [ qw/username name password / ], [ 'jgda', 'Jonas Alves', ''], diff --git a/t/lib/DBSchema/Result/Address.pm b/t/lib/DBSchema/Result/Address.pm new file mode 100644 index 0000000..e5a36ba --- /dev/null +++ b/t/lib/DBSchema/Result/Address.pm @@ -0,0 +1,32 @@ +package DBSchema::Result::Address; + +use strict; +use warnings; + +use base 'DBIx::Class'; + +__PACKAGE__->load_components("Core"); +__PACKAGE__->table("address"); +__PACKAGE__->add_columns( + "address_id", + { data_type => "INTEGER", is_auto_increment => 1, + is_nullable => 0 }, + "user_id", + { data_type => "INTEGER", is_nullable => 0 }, + "street", + { data_type => "VARCHAR", is_nullable => 0, size => 32 }, + "city", + { data_type => "VARCHAR", is_nullable => 0, size => 32 }, + "state", + { data_type => "VARCHAR", is_nullable => 0, size => 32 }, +); +__PACKAGE__->set_primary_key("address_id"); + +__PACKAGE__->belongs_to( + 'user', + 'DBSchema::Result::User', + 'user_id', +); + + +1; diff --git a/t/lib/DBSchema/Result/User.pm b/t/lib/DBSchema/Result/User.pm index b8f53a5..363271d 100644 --- a/t/lib/DBSchema/Result/User.pm +++ b/t/lib/DBSchema/Result/User.pm @@ -38,5 +38,11 @@ __PACKAGE__->has_many( ); __PACKAGE__->many_to_many('roles', 'user_roles' => 'role'); +__PACKAGE__->might_have( + "address", + "DBSchema::Result::Address", + { 'foreign.user_id' => 'self.id' } +); + 1; diff --git a/t/lib/RunTests.pm b/t/lib/RunTests.pm index 4557e01..fd4fe71 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 => 28; + plan tests => 29; my $dvd_rs = $schema->resultset( 'Dvd' ); my $user_rs = $schema->resultset( 'User' ); @@ -133,6 +133,20 @@ sub run_tests{ is( scalar @tags, 2, 'Tags in has_many related record saved' ); ok( $owned_dvds{'temp name 2'}, 'Second name in a has_many related record saved' ); + $updates = { + id => $user->id, + address => { + street => "101 Main Street", + city => "Podunk", + state => "New York" + } + }; + $user = $user_rs->recursive_update( $updates ); + $user = $user_rs->recursive_update( $updates ); + is( $schema->resultset( 'Address' )->search({ user_id => $user->id })->count, 1, + 'the right number of addresses' ); + + # $updates = { # name => 'Test name 1', # }; diff --git a/t/sqlite.t b/t/sqlite.t index 2d2a041..ca3e72e 100644 --- a/t/sqlite.t +++ b/t/sqlite.t @@ -5,7 +5,7 @@ use DBSchema; use RunTests; use Test::More; -unlink 't/var/dvdzbr.db'; +#unlink 't/var/dvdzbr.db'; my $schema = DBSchema::get_test_schema(); run_tests( $schema ); diff --git a/t/var/dvdzbr.db b/t/var/dvdzbr.db index 318983a..73c1fcd 100644 Binary files a/t/var/dvdzbr.db and b/t/var/dvdzbr.db differ