From: John Napiorkowski <jjn1056@yahoo.com>
Date: Mon, 25 Jun 2007 22:55:24 +0000 (+0000)
Subject: Added multi-create object support to Schema->populate and created a test for this... 
X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8b93a938b08b80cea6b779449d9879c350ea7656;p=dbsrgits%2FDBIx-Class-Historic.git

Added multi-create object support to Schema->populate and created a test for this.  Also added my name to the contributors list so my wife believes me when I tell her what I'm working on in my offtime :)
---

diff --git a/lib/DBIx/Class.pm b/lib/DBIx/Class.pm
index 42742f2..09953b1 100644
--- a/lib/DBIx/Class.pm
+++ b/lib/DBIx/Class.pm
@@ -222,6 +222,8 @@ jesper: Jesper Krogh
 
 jguenther: Justin Guenther <jguenther@cpan.org>
 
+jnapiorkowski: John Napiorkowski <jjn1056@yahoo.com>
+
 jshirley: J. Shirley <jshirley@gmail.com>
 
 konobi: Scott McWhirter
diff --git a/lib/DBIx/Class/Schema.pm b/lib/DBIx/Class/Schema.pm
index 99b7f28..db2a8f6 100644
--- a/lib/DBIx/Class/Schema.pm
+++ b/lib/DBIx/Class/Schema.pm
@@ -851,7 +851,15 @@ sub populate {
     }
     return @created;
   }
-  $self->storage->insert_bulk($self->source($name), \@names, $data);
+  my @results_to_create;
+  foreach my $datum (@$data) {
+    my %result_to_create;
+    foreach my $index (0..$#names) {
+      $result_to_create{$names[$index]} = $$datum[$index];
+    }
+    push @results_to_create, \%result_to_create;
+  }
+  $rs->populate(\@results_to_create);
 }
 
 =head2 exception_action
diff --git a/t/101populate_rs.t b/t/101populate_rs.t
index 5ea3a55..9bbe60f 100644
--- a/t/101populate_rs.t
+++ b/t/101populate_rs.t
@@ -15,7 +15,7 @@ use Test::More;
 use lib qw(t/lib);
 use DBICTest;
 
-plan tests => 98;
+plan tests => 120;
 
 
 ## ----------------------------------------------------------------------------
@@ -32,6 +32,76 @@ ok( $cd_rs, 'Got Good CD Resultset');
 
 
 ## ----------------------------------------------------------------------------
+## Schema populate Tests
+## ----------------------------------------------------------------------------
+
+SCHEMA: {
+
+	## Test to make sure that the old $schema->populate is using the new method
+	## for $resultset->populate when in void context and with sub objects.
+	
+	$schema->populate('Artist', [
+	
+		[qw/name cds/],
+		["001First Artist", [
+			{title=>"001Title1", year=>2000},
+			{title=>"001Title2", year=>2001},
+			{title=>"001Title3", year=>2002},
+		]],
+		["002Second Artist", []],
+		["003Third Artist", [
+			{title=>"003Title1", year=>2005},
+		]],
+	]);
+	
+	isa_ok $schema, 'DBIx::Class::Schema';
+	
+	my ($artist1, $artist2, $artist3) = $schema->resultset('Artist')->search({
+		name=>["001First Artist","002Second Artist","003Third Artist"]},
+		{order_by=>'name ASC'})->all;
+	
+	isa_ok  $artist1, 'DBICTest::Artist';
+	isa_ok  $artist2, 'DBICTest::Artist';
+	isa_ok  $artist3, 'DBICTest::Artist';
+	
+	ok $artist1->name eq '001First Artist', "Got Expected Artist Name for Artist001";
+	ok $artist2->name eq '002Second Artist', "Got Expected Artist Name for Artist002";
+	ok $artist3->name eq '003Third Artist', "Got Expected Artist Name for Artist003";
+	
+	ok $artist1->cds->count eq 3, "Got Right number of CDs for Artist1";
+	ok $artist2->cds->count eq 0, "Got Right number of CDs for Artist2";
+	ok $artist3->cds->count eq 1, "Got Right number of CDs for Artist3";
+	
+	ARTIST1CDS: {
+	
+		my ($cd1, $cd2, $cd3) = $artist1->cds->search(undef, {order_by=>'year ASC'});
+		
+		isa_ok $cd1, 'DBICTest::CD';
+		isa_ok $cd2, 'DBICTest::CD';
+		isa_ok $cd3, 'DBICTest::CD';
+		
+		ok $cd1->year == 2000;
+		ok $cd2->year == 2001;
+		ok $cd3->year == 2002;
+		
+		ok $cd1->title eq '001Title1';
+		ok $cd2->title eq '001Title2';
+		ok $cd3->title eq '001Title3';
+	}
+	
+	ARTIST3CDS: {
+	
+		my ($cd1) = $artist3->cds->search(undef, {order_by=>'year ASC'});
+		
+		isa_ok $cd1, 'DBICTest::CD';
+
+		ok $cd1->year == 2005;
+		ok $cd1->title eq '003Title1';
+	}	
+}
+
+
+## ----------------------------------------------------------------------------
 ## Array context tests
 ## ----------------------------------------------------------------------------