[Bast-commits] r3353 - in branches/DBIx-Class/bulk_create: lib/DBIx/Class t

jnapiorkowski at dev.catalyst.perl.org jnapiorkowski at dev.catalyst.perl.org
Tue May 22 00:47:57 GMT 2007


Author: jnapiorkowski
Date: 2007-05-22 00:47:56 +0100 (Tue, 22 May 2007)
New Revision: 3353

Modified:
   branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSet.pm
   branches/DBIx-Class/bulk_create/t/101populate_rs.t
Log:
-- added support for belongs_to type relationships and better support for when the relating keys are autogenerated.  
-- more tests.

Modified: branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSet.pm
===================================================================
--- branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSet.pm	2007-05-21 22:29:11 UTC (rev 3352)
+++ branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSet.pm	2007-05-21 23:47:56 UTC (rev 3353)
@@ -34,7 +34,7 @@
 
   package MyApp::Schema::Artist;
   use base qw/DBIx::Class/;
-  __PACKAGE__->load_components(qw/Core/);
+  __PACKAGE__->load_components(qw/Core/)
   __PACKAGE__->table('artist');
   __PACKAGE__->add_columns(qw/artistid name/);
   __PACKAGE__->set_primary_key('artistid');
@@ -1291,7 +1291,7 @@
   print $ArtistThree->cds->count ## reponse is '2'
 
 =cut
-
+use Data::Dump qw/dump/;
 sub populate {
   my ($self, $data) = @_;
   
@@ -1304,8 +1304,35 @@
   } else {
     my ($first, @rest) = @$data;
 
-    my @names = grep { !ref $first->{$_} } keys %$first;
+	my @names = grep {!ref $first->{$_}} keys %$first;
+    my @rels = grep { $self->result_source->has_relationship($_) } keys %$first;
+    my @pks = $self->result_source->primary_columns;	
 
+	## do the belongs_to relationships	
+    foreach my $index (0..$#{@$data})
+	{
+		foreach my $rel (@rels)
+		{
+			next unless $data->[$index]->{$rel} && ref $data->[$index]->{$rel} eq "HASH";
+			
+			my $result = $self->related_resultset($rel)->create($data->[$index]->{$rel});
+			
+			my ($reverse) = keys %{$self->result_source->reverse_relationship_info($rel)};
+			
+			my $related = $result->result_source->resolve_condition(
+
+				$result->result_source->relationship_info($reverse)->{cond},
+				$self,				
+				$result,				
+			);
+
+			delete $data->[$index]->{$rel};
+			$data->[$index] = {%{$data->[$index]}, %$related};
+			
+			push @names, keys %$related if $index == 0;
+		}
+	}
+	
     my @values = map {
       [ map {
          defined $_ ? $_ : $self->throw_exception("Undefined value for column!")
@@ -1318,13 +1345,11 @@
       \@values,
     );
 
-    my @rels = grep { $self->result_source->has_relationship($_) } keys %$first;
-    my @pks = $self->result_source->primary_columns;
-
+	## do the has_many relationships
     foreach my $item (@$data) {
 
       foreach my $rel (@rels) {
-        next unless $item->{$rel};
+        next unless $item->{$rel} && ref $item->{$rel} eq "ARRAY";
 
         my $parent = $self->find(map {{$_=>$item->{$_}} } @pks) || next;
         my $child = $parent->$rel;

Modified: branches/DBIx-Class/bulk_create/t/101populate_rs.t
===================================================================
--- branches/DBIx-Class/bulk_create/t/101populate_rs.t	2007-05-21 22:29:11 UTC (rev 3352)
+++ branches/DBIx-Class/bulk_create/t/101populate_rs.t	2007-05-21 23:47:56 UTC (rev 3353)
@@ -5,7 +5,7 @@
 use lib qw(t/lib);
 use DBICTest;
 
-plan tests => 43;
+plan tests => 53;
 
 my $schema = DBICTest->init_schema();
 my $rs = $schema->resultset('Artist');
@@ -63,7 +63,7 @@
 		]
 	  },
 	  { artistid =>10,  name => 'XXXX' },
-	  { artistid =>11, name => 'wart', cds =>{ title => 'xxxaaa' ,year => 2005 }, },
+	  { artistid =>11, name => 'wart', cds =>[{ title => 'xxxaaa' ,year => 2005 }], },
 	] );
 	
 	my $artist = $rs->find(8);
@@ -182,8 +182,42 @@
 	is($cdB->artist->name, 'Fred BloggsB', 'Set Artist to FredB');
 }
 
+RETURN_VOID_BELONGS_TO: {
 
+	## Test from a belongs_to perspective, should create artist first, then CD with artistid in:
+	
+	my $cds = [
+		{
+			title => 'Some CD3',
+			year => '1997',
+			artist => { name => 'Fred BloggsC'},
+		},
+		{
+			title => 'Some CD4',
+			year => '1997',
+			artist => { name => 'Fred BloggsD'},
+		},		
+	];
+	
+	my $cd_rs = $schema->resultset('CD');
+	
+	ok( $cd_rs, 'Got Good CD Resultset');
+	
+	$cd_rs->populate($cds);
+	
+	my $cdA = $schema->resultset('CD')->find({title => 'Some CD3'});
 
+	isa_ok($cdA, 'DBICTest::CD', 'Created CD');
+	isa_ok($cdA->artist, 'DBICTest::Artist', 'Set Artist');
+	is($cdA->artist->name, 'Fred BloggsC', 'Set Artist to FredC');
 
+	my $cdB = $schema->resultset('CD')->find({title => 'Some CD4'});
+	
+	isa_ok($cdB, 'DBICTest::CD', 'Created CD');
+	isa_ok($cdB->artist, 'DBICTest::Artist', 'Set Artist');
+	is($cdB->artist->name, 'Fred BloggsD', 'Set Artist to FredD');
+}
 
 
+
+




More information about the Bast-commits mailing list