From: Peter Rabbitson Date: Fri, 3 Jul 2009 14:36:32 +0000 (+0000) Subject: Extra test to demonstrate has_one working, and a POD clarification of multicreate X-Git-Tag: v0.08108~13 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9c3018b6b589a8250b75a3db451a5baac9c6c3df;p=dbsrgits%2FDBIx-Class.git Extra test to demonstrate has_one working, and a POD clarification of multicreate --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 58ccaa3..4f7dd00 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -2226,12 +2226,15 @@ store. If the appropriate relationships are set up, foreign key fields can also be passed an object representing the foreign row, and the value will be set to its primary key. -To create related objects, pass a hashref for the value if the related -item is a foreign key relationship (L), -and use the name of the relationship as the key. (NOT the name of the field, -necessarily). For C and C relationships, pass an arrayref -of hashrefs containing the data for each of the rows to create in the foreign -tables, again using the relationship name as the key. +To create related objects, pass a hashref of related-object column values +B. If the relationship is of type C +(L) - pass an arrayref of hashrefs. +The process will correctly identify columns holding foreign keys, and will +transparrently populate them from the keys of the corresponding relation. +This can be applied recursively, and will work correctly for a structure +with an arbitrary depth and width, as long as the relationships actually +exists and the correct column data has been supplied. + Instead of hashrefs of plain related data (key/value pairs), you may also pass new or inserted objects. New objects (not inserted yet, see diff --git a/t/lib/DBICTest/Schema/Artwork.pm b/t/lib/DBICTest/Schema/Artwork.pm index 849096b..4eecef5 100644 --- a/t/lib/DBICTest/Schema/Artwork.pm +++ b/t/lib/DBICTest/Schema/Artwork.pm @@ -7,6 +7,7 @@ __PACKAGE__->table('cd_artwork'); __PACKAGE__->add_columns( 'cd_id' => { data_type => 'integer', + is_nullable => 0, }, ); __PACKAGE__->set_primary_key('cd_id'); diff --git a/t/multi_create/multilev_single_PKeqFK.t b/t/multi_create/multilev_single_PKeqFK.t index c1c7c1c..07101a2 100644 --- a/t/multi_create/multilev_single_PKeqFK.t +++ b/t/multi_create/multilev_single_PKeqFK.t @@ -8,7 +8,7 @@ use DBICTest; sub mc_diag { diag (@_) if $ENV{DBIC_MULTICREATE_DEBUG} }; -plan tests => 16; +plan tests => 26; my $schema = DBICTest->init_schema(); @@ -30,8 +30,30 @@ my $rels = { might_have => 'artwork', }; -my $artist_rs = $schema->resultset('Artist'); +for my $type (qw/has_one might_have/) { + + lives_ok (sub { + + my $rel = $rels->{$type}; + my $cd_title = "Simple test $type cd"; + my $cd = $schema->resultset('CD')->create ({ + artist => 1, + title => $cd_title, + year => 2008, + $rel => {}, + }); + + isa_ok ($cd, 'DBICTest::CD', 'Main CD object created'); + is ($cd->title, $cd_title, 'Correct CD title'); + + isa_ok ($cd->$rel, 'DBICTest::Artwork', 'Related artwork present'); + ok ($cd->$rel->in_storage, 'And in storage'); + + }, "Simple $type creation"); +} + +my $artist_rs = $schema->resultset('Artist'); for my $type (qw/has_one might_have/) { my $rel = $rels->{$type};