=head2 set_from_related
$book->set_from_related('author', $author_obj);
+ $book->author($author_obj); ## same thing
Set column values on the current object, using related values from the given
related object. This is used to associate previously separate objects, for
example, to set the correct author for a book, find the Author object, then
call set_from_related on the book.
+This is called internally when you pass existing objects as values to
+L<DBIx::Class::ResultSet/create>, or pass an object to a belongs_to acessor.
+
The columns are only set in the local copy of the object, call L</update> to
set them in the storage.
} elsif ($info && $info->{attrs}{accessor}
&& $info->{attrs}{accessor} eq 'multi'
&& ref $attrs->{$key} eq 'ARRAY') {
- my $others = $attrs->{$key};
+ my $others = delete $attrs->{$key};
$new->{_rel_in_storage} = 1;
foreach my $rel_obj (@$others) {
if(!Scalar::Util::blessed($rel_obj)) {
$new->{_rel_in_storage} = 0;
}
}
- $new->set_from_related($key, $others);
- $related->{$key} = $attrs->{$key};
+# $new->set_from_related($key, $others);
+ $related->{$key} = $others;
+ next;
} elsif ($class->has_column($key)
&& exists $class->column_info($key)->{_inflate_info})
{
}
}
- $source->storage->insert($source->from, { $self->get_columns });
+ $source->storage->insert($source, { $self->get_columns });
+
+ ## PK::Auto
+ my ($pri, $too_many) = grep { !defined $self->get_column($_) ||
+ ref($self->get_column($_)) eq 'SCALAR'} $self->primary_columns;
+ if(defined $pri) {
+ $self->throw_exception( "More than one possible key found for auto-inc on ".ref $self )
+ if defined $too_many;
+
+ my $storage = $self->result_source->storage;
+ $self->throw_exception( "Missing primary key but Storage doesn't support last_insert_id" )
+ unless $storage->can('last_insert_id');
+ my $id = $storage->last_insert_id($self->result_source,$pri);
+ $self->throw_exception( "Can't get last insert id" ) unless $id;
+ $self->store_column($pri => $id);
+ }
foreach my $relname (keys %related_stuff) {
my $relobj = $related_stuff{$relname};
my $schema = DBICTest->init_schema();
-plan tests => 1;
-
-my $artist = $schema->resultset('Artist')->create({ name => 'Fred 1'});
-
-my $cd = $schema->resultset('CD')->create({ artist => $artist,
- title => 'Some CD',
- year => 1996
- });
+plan tests => 4;
my $cd2 = $schema->resultset('CD')->create({ artist =>
{ name => 'Fred Bloggs' },
year => 1996
});
-is(ref $cd->artist, 'DBICTest::Artist', 'Created CD and Artist object');
+is(ref $cd2->artist, 'DBICTest::Artist', 'Created CD and Artist object');
+is($cd2->artist->name, 'Fred Bloggs', 'Artist created correctly');
+
+my $artist = $schema->resultset('Artist')->create({ name => 'Fred 2',
+ cds => [
+ { title => 'Music to code by',
+ year => 2007,
+ },
+ ],
+ });
+is(ref $artist->cds->first, 'DBICTest::CD', 'Created Artist with CDs');
+is($artist->cds->first->title, 'Music to code by', 'CD created correctly');