if $self->can('result_source_instance');
$self->throw_exception("No result_source set on this object; can't insert")
unless $source;
- #use Data::Dumper; warn Dumper($self);
+
- $source->storage->insert($source, { $self->get_columns });
+ # Check if we stored uninserted relobjs here in new()
+ $source->storage->txn_begin if(!$self->{_rel_in_storage});
+
+ my %related_stuff = (%{$self->{_relationship_data} || {}},
+ %{$self->{_inflated_column} || {}});
+ ## Should all be in relationship_data, but we need to get rid of the
+ ## 'filter' reltype..
+ foreach my $relname (keys %related_stuff) {
+ my $relobj = $related_stuff{$relname};
+ if(ref $relobj ne 'ARRAY') {
+ $relobj->insert() if(!$relobj->in_storage);
+ $self->set_from_related($relname, $relobj);
+ }
+ }
+
+ $source->storage->insert($source->from, { $self->get_columns });
+
+ foreach my $relname (keys %related_stuff) {
+ my $relobj = $related_stuff{$relname};
+ if(ref $relobj eq 'ARRAY') {
+ foreach my $obj (@$relobj) {
+ my $info = $self->relationship_info($relname);
+ ## What about multi-col FKs ?
+ my $key = $1 if($info && (keys %{$info->{cond}})[0] =~ /^foreign\.(\w+)/);
+ $obj->set_from_related($key, $self);
+ $obj->insert() if(!$obj->in_storage);
+ }
+ }
+ }
+ $source->storage->txn_commit if(!$self->{_rel_in_storage});
+
$self->in_storage(1);
$self->{_dirty_columns} = {};
$self->{related_resultsets} = {};