[Bast-commits] r3422 - in branches/DBIx-Class-current: .
lib/DBIx/Class t
matthewt at dev.catalyst.perl.org
matthewt at dev.catalyst.perl.org
Tue May 29 02:08:35 GMT 2007
Author: matthewt
Date: 2007-05-29 02:08:35 +0100 (Tue, 29 May 2007)
New Revision: 3422
Modified:
branches/DBIx-Class-current/
branches/DBIx-Class-current/lib/DBIx/Class/ResultSet.pm
branches/DBIx-Class-current/t/101populate_rs.t
Log:
r51761 at cain (orig r3353): jnapiorkowski | 2007-05-21 23:47:56 +0000
-- added support for belongs_to type relationships and better support for when the relating keys are autogenerated.
-- more tests.
Property changes on: branches/DBIx-Class-current
___________________________________________________________________
Name: svk:merge
- 168d5346-440b-0410-b799-f706be625ff1:/DBIx-Class-current:2207
462d4d0c-b505-0410-bf8e-ce8f877b3390:/local/bast/DBIx-Class:3159
78d5d833-3a7e-474d-aed6-cfba645156b5:/local/DBIx-Class.bulk_create:29204
78d5d833-3a7e-474d-aed6-cfba645156b5:/local/DBIx-Class.current:29201
78d5d833-3a7e-474d-aed6-cfba645156b5:/local/DBIx-Class.oracle8:29250
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-C3:318
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-joins:173
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-resultset:570
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/bulk_create:3350
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/collapse_result_rewrite:3292
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/column_info_from_storage:2596
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/datetime:1716
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/find_compat:1855
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/find_unique_query_fixes:2142
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/inflate:1988
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/load_namespaces:2725
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/many_to_many:2025
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/param_bind:3015
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/re_refactor_bugfix:1944
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/reorganize_tests:1827
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset-new-refactor:1766
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset_2_electric_boogaloo:2175
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset_cleanup:2102
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/source-handle:2975
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/sqlt_tests_refactor:2043
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/storage_exceptions:2617
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/versioning:2930
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/trunk/DBIx-Class:3355
+ 168d5346-440b-0410-b799-f706be625ff1:/DBIx-Class-current:2207
462d4d0c-b505-0410-bf8e-ce8f877b3390:/local/bast/DBIx-Class:3159
78d5d833-3a7e-474d-aed6-cfba645156b5:/local/DBIx-Class.bulk_create:29204
78d5d833-3a7e-474d-aed6-cfba645156b5:/local/DBIx-Class.current:29201
78d5d833-3a7e-474d-aed6-cfba645156b5:/local/DBIx-Class.oracle8:29250
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-C3:318
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-joins:173
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-resultset:570
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/bulk_create:3353
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/collapse_result_rewrite:3292
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/column_info_from_storage:2596
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/datetime:1716
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/find_compat:1855
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/find_unique_query_fixes:2142
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/inflate:1988
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/load_namespaces:2725
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/many_to_many:2025
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/param_bind:3015
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/re_refactor_bugfix:1944
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/reorganize_tests:1827
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset-new-refactor:1766
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset_2_electric_boogaloo:2175
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset_cleanup:2102
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/source-handle:2975
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/sqlt_tests_refactor:2043
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/storage_exceptions:2617
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/versioning:2930
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/trunk/DBIx-Class:3355
Modified: branches/DBIx-Class-current/lib/DBIx/Class/ResultSet.pm
===================================================================
--- branches/DBIx-Class-current/lib/DBIx/Class/ResultSet.pm 2007-05-29 01:08:25 UTC (rev 3421)
+++ branches/DBIx-Class-current/lib/DBIx/Class/ResultSet.pm 2007-05-29 01:08:35 UTC (rev 3422)
@@ -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-current/t/101populate_rs.t
===================================================================
--- branches/DBIx-Class-current/t/101populate_rs.t 2007-05-29 01:08:25 UTC (rev 3421)
+++ branches/DBIx-Class-current/t/101populate_rs.t 2007-05-29 01:08:35 UTC (rev 3422)
@@ -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