Add PK::Auto to Row/insert for now, could be prettier
Jess Robinson [Tue, 6 Feb 2007 19:38:35 +0000 (19:38 +0000)]
lib/DBIx/Class/Relationship/Base.pm
lib/DBIx/Class/Row.pm
t/96multi_create.t

index 8409165..c865642 100644 (file)
@@ -317,12 +317,16 @@ sub update_or_create_related {
 =head2 set_from_related
 
   $book->set_from_related('author', $author_obj);
+  $book->author($author_obj);                      ## same thing
 
 Set column values on the current object, using related values from the given
 related object. This is used to associate previously separate objects, for
 example, to set the correct author for a book, find the Author object, then
 call set_from_related on the book.
 
+This is called internally when you pass existing objects as values to
+L<DBIx::Class::ResultSet/create>, or pass an object to a belongs_to acessor.
+
 The columns are only set in the local copy of the object, call L</update> to
 set them in the storage.
 
index 0fde913..dc1312d 100644 (file)
@@ -76,7 +76,7 @@ sub new {
         } elsif ($info && $info->{attrs}{accessor}
             && $info->{attrs}{accessor} eq 'multi'
             && ref $attrs->{$key} eq 'ARRAY') {
-            my $others = $attrs->{$key};
+            my $others = delete $attrs->{$key};
             $new->{_rel_in_storage} = 1;
             foreach my $rel_obj (@$others) {
               if(!Scalar::Util::blessed($rel_obj)) {
@@ -84,8 +84,9 @@ sub new {
                 $new->{_rel_in_storage} = 0;
               }
             }
-            $new->set_from_related($key, $others);
-            $related->{$key} = $attrs->{$key};
+#            $new->set_from_related($key, $others);
+            $related->{$key} = $others;
+            next;
         } elsif ($class->has_column($key)
           && exists $class->column_info($key)->{_inflate_info})
         {
@@ -151,7 +152,22 @@ sub insert {
     }
   }
 
-  $source->storage->insert($source->from, { $self->get_columns });
+  $source->storage->insert($source, { $self->get_columns });
+
+  ## PK::Auto
+  my ($pri, $too_many) = grep { !defined $self->get_column($_) || 
+                                    ref($self->get_column($_)) eq 'SCALAR'} $self->primary_columns;
+  if(defined $pri) {
+    $self->throw_exception( "More than one possible key found for auto-inc on ".ref $self )
+      if defined $too_many;
+
+    my $storage = $self->result_source->storage;
+    $self->throw_exception( "Missing primary key but Storage doesn't support last_insert_id" )
+      unless $storage->can('last_insert_id');
+    my $id = $storage->last_insert_id($self->result_source,$pri);
+    $self->throw_exception( "Can't get last insert id" ) unless $id;
+    $self->store_column($pri => $id);
+  }
 
   foreach my $relname (keys %related_stuff) {
     my $relobj = $related_stuff{$relname};
index 205ba4f..f7f5602 100644 (file)
@@ -7,14 +7,7 @@ use DBICTest;
 
 my $schema = DBICTest->init_schema();
 
-plan tests => 1;
-
-my $artist = $schema->resultset('Artist')->create({ name => 'Fred 1'});
-
-my $cd = $schema->resultset('CD')->create({ artist => $artist,
-                                            title => 'Some CD',
-                                            year => 1996
-                                          });
+plan tests => 4;
 
 my $cd2 = $schema->resultset('CD')->create({ artist => 
                                    { name => 'Fred Bloggs' },
@@ -22,4 +15,15 @@ my $cd2 = $schema->resultset('CD')->create({ artist =>
                                    year => 1996
                                  });
 
-is(ref $cd->artist, 'DBICTest::Artist', 'Created CD and Artist object');
+is(ref $cd2->artist, 'DBICTest::Artist', 'Created CD and Artist object');
+is($cd2->artist->name, 'Fred Bloggs', 'Artist created correctly');
+
+my $artist = $schema->resultset('Artist')->create({ name => 'Fred 2',
+                                                     cds => [
+                                                             { title => 'Music to code by',
+                                                               year => 2007,
+                                                             },
+                                                             ],
+                                                     });
+is(ref $artist->cds->first, 'DBICTest::CD', 'Created Artist with CDs');
+is($artist->cds->first->title, 'Music to code by', 'CD created correctly');