]> Dogcows Code - chaz/p5-DBIx-Class-ResultSet-RecursiveUpdate/commitdiff
might_have with non pk fk
authorzby <zby@bd8105ee-0ff8-0310-8827-fb3f25b6796d>
Mon, 4 May 2009 22:30:59 +0000 (22:30 +0000)
committerzby <zby@bd8105ee-0ff8-0310-8827-fb3f25b6796d>
Mon, 4 May 2009 22:30:59 +0000 (22:30 +0000)
lib/DBIx/Class/ResultSet/RecursiveUpdate.pm
t/lib/DBSchema.pm
t/lib/DBSchema/Result/Address.pm [new file with mode: 0644]
t/lib/DBSchema/Result/User.pm
t/lib/RunTests.pm
t/sqlite.t
t/var/dvdzbr.db

index ca6a40da24eab5247e5ae6061d2574ef9b3a9360..884cf0584d0ebfdef4672ebe30617a1949a2e4da 100644 (file)
@@ -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 );
     }
 }
index f44ee458fdfa6babc17915f54b7372ef01ed1776..e72915541475c51af4f6e74e160743d516eeba49 100644 (file)
@@ -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 (file)
index 0000000..e5a36ba
--- /dev/null
@@ -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;
index b8f53a5da0b9f30abcdad3de8dbf8009a78c2483..363271daafc067def1ddc99e8aec81f85eb5146b 100644 (file)
@@ -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;
 
index 4557e016975f31c21552dd7941ff862687bf14dd..fd4fe71d9cf655003c477a1c0767fc6696952247 100644 (file)
@@ -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',
 #    };
index 2d2a041416e64b7ead8eaf672176abed7536938a..ca3e72e494115cd2ead4661df7b98342f047501b 100644 (file)
@@ -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 );
 
index 318983a2c0da7f687115882d1ffb28ea29b02613..73c1fcd598d63602623953b284c2cc14118a6dae 100644 (file)
Binary files a/t/var/dvdzbr.db and b/t/var/dvdzbr.db differ
This page took 0.026933 seconds and 4 git commands to generate.