From f6641f7ade5a9b4227feef8a538228fcea80a5b2 Mon Sep 17 00:00:00 2001 From: zby Date: Mon, 4 May 2009 22:30:59 +0000 Subject: [PATCH] might_have with non pk fk --- lib/DBIx/Class/ResultSet/RecursiveUpdate.pm | 19 ++++++++---- t/lib/DBSchema.pm | 2 +- t/lib/DBSchema/Result/Address.pm | 32 ++++++++++++++++++++ t/lib/DBSchema/Result/User.pm | 6 ++++ t/lib/RunTests.pm | 16 +++++++++- t/sqlite.t | 2 +- t/var/dvdzbr.db | Bin 19456 -> 37888 bytes 7 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 t/lib/DBSchema/Result/Address.pm 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 318983a2c0da7f687115882d1ffb28ea29b02613..73c1fcd598d63602623953b284c2cc14118a6dae 100644 GIT binary patch literal 37888 zcmeHQ|8Eq>6`$F6dwV|W&~P}1L8tIZ>s#fGT`a{((kt+2c=pWEjtrXN>iWDiLRU=htrABR41r*Uhf9ac<-Sw`0 zXB?#VlWyHH&wAAbyKauM1mm1NZ<>s;@8efoPvDp1 z_j%t(PhNtyp-V?DfLnrhKl~T&z%BUKh7zu2nn@2fLtrVTh#Y@G4TfAJTFL@{^WnHls96#4&(_90F zFhfDGyo8Y*&+tYjmf755wdz#rg;}p!^_HBfpRwLF<7UiZA*&;;Miq=~Cd1!+scz4f z9fd=}&v<(5g_Gk-+_Y)BMFeJg{F~!b=IN=4$+4*!^QG|_^VG}J=BZaso*Y0HA%l6z zuFjpatGWE}uqBA@LU9)1UP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