Reputation: 7825
I would like to construct a DBIx::Class object and its related objects but defer saving the object, calling the insert
method on the object later and at that time have everything saved all at once.
Here is example code based on the examples from the DBIx::Class documentation (artist, cd, etc.).
use MyApp::Schema;
use Data::Dumper ;
sub make_artist_from_some_other_object
{
my ($object, $schema) = @_ ;
my $artist = $schema->resultset('Artist')->new({});
$artist->name($object->firstname() . ' ' . $object->lastname()) ;
$artist->new_related('cds', {'title' => $object->firsttitle(),
'year' => $object->firstyear()}) ;
$artist->new_related('cds', {'title' => $object->secondtitle(),
'year' => $object->secondyear()}) ;
$artist->new_related('cds', {'title' => $object->thirdtitle(),
'year' => $object->thirdyear()}) ;
return $artist ;
}
my $schema = MyApp::Schema->connect('dbi:SQLite:dbname=/tmp/abcd.sqlite');
$artist = make_artist_from_some_other_object($some_object, $schema) ;
$artist->insert() ;
# The insert does _not_ save cd information.
I realize there are ways around this, e.g., keeping the related objects around and then saving them individually, but what I am looking for is a built-in DBIx::Class way to do all the object construction beforehand and then do a single insert sometime later. Is this possible?
Upvotes: 0
Views: 157
Reputation: 2204
There is currently no way to do that.
You can only create separate objects with new_result
and insert them later with insert.
Often the primary key(s) are assigned by the database so they aren't available before that.
You can try to populate the relationship cache to achieve what you want.
What's your reason to not immediately insert all rows?
Upvotes: 1