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);
- # Check if we stored uninserted relobjs here in new()
- $source->storage->txn_begin if(!$self->{_rel_in_storage});
# Check if we stored uninserted relobjs here in new()
my %related_stuff = (%{$self->{_relationship_data} || {}},
$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 @auto_pri = grep {
+ !defined $self->get_column($_) ||
+ ref($self->get_column($_)) eq 'SCALAR'
+ } $self->primary_columns;
+
+ if (@auto_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);
+ my @ids = $storage->last_insert_id($self->result_source,@auto_pri);
+ $self->throw_exception( "Can't get last insert id" )
+ unless (@ids == @auto_pri);
+ $self->store_column($auto_pri[$_] => $ids[$_]) for 0 .. $#ids;
}
if(!$self->{_rel_in_storage})
Also takes an options hashref of C<< column_name => value> pairs >> to update
first. But be aware that this hashref might be edited in place, so dont rely on
-it being the same after a call to C<update>.
+it being the same after a call to C<update>. If you need to preserve the hashref,
+it is sufficient to pass a shallow copy to C<update>, e.g. ( { %{ $href } } )
=cut
sub throw_exception {
my $self=shift;
- if (ref $self && ref $self->result_source) {
+ if (ref $self && ref $self->result_source && $self->result_source->schema) {
$self->result_source->schema->throw_exception(@_);
} else {
croak(@_);