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');
print $ArtistThree->cds->count ## reponse is '2'
=cut
-
+use Data::Dump qw/dump/;
sub populate {
my ($self, $data) = @_;
} 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!")
\@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;
use lib qw(t/lib);
use DBICTest;
-plan tests => 43;
+plan tests => 53;
my $schema = DBICTest->init_schema();
my $rs = $schema->resultset('Artist');
]
},
{ 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);
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');
+}