From: John Napiorkowski Date: Mon, 21 May 2007 23:47:56 +0000 (+0000) Subject: -- added support for belongs_to type relationships and better support for when the... X-Git-Tag: v0.08010~150^2~50^2~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=dc87edb5845de612331948bce2445a3246663e96;p=dbsrgits%2FDBIx-Class.git -- added support for belongs_to type relationships and better support for when the relating keys are autogenerated. -- more tests. --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index df5b998..31358a9 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -34,7 +34,7 @@ In the examples below, the following table classes are used: 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 @@ Example: Assuming an Artist Class that has many CDs Classes relating: print $ArtistThree->cds->count ## reponse is '2' =cut - +use Data::Dump qw/dump/; sub populate { my ($self, $data) = @_; @@ -1304,8 +1304,35 @@ sub populate { } 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 @@ sub populate { \@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; diff --git a/t/101populate_rs.t b/t/101populate_rs.t index 9f6e655..c1f497f 100644 --- a/t/101populate_rs.t +++ b/t/101populate_rs.t @@ -5,7 +5,7 @@ use Test::More; 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 @@ RETURN_VOID_HAS_MANY: { ] }, { 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,7 +182,41 @@ RETURN_RESULTSETS_BELONGS_TO: { 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'); +}