sub _siblings {
my $self = shift;
my $position_column = $self->position_column;
- return defined (my $pos = $self->get_column($position_column))
+ my $pos;
+ return defined ($pos = $self->get_column($position_column))
? $self->_group_rs->search(
- { $position_column => { '!=' => $self->get_column($position_column) } },
+ { $position_column => { '!=' => $pos } },
)
: $self->_group_rs
;
}
sub _ident_values {
- my ($self) = @_;
+ my ($self, $use_storage_state) = @_;
my (@ids, @missing);
for ($self->_pri_cols) {
- push @ids, exists $self->{_column_data_in_storage}{$_}
+ push @ids, ($use_storage_state and exists $self->{_column_data_in_storage}{$_})
? $self->{_column_data_in_storage}{$_}
: $self->get_column($_)
;
=cut
sub ident_condition {
- my ($self, $alias) = @_;
+ shift->_mk_ident_cond(@_);
+}
+
+sub _storage_ident_condition {
+ shift->_mk_ident_cond(shift, 1);
+}
+
+sub _mk_ident_cond {
+ my ($self, $alias, $use_storage_state) = @_;
my @pks = $self->_pri_cols;
- my @vals = $self->_ident_values;
+ my @vals = $self->_ident_values($use_storage_state);
my (%cond, @undef);
my $prefix = defined $alias ? $alias.'.' : '';
$self->throw_exception( "Not in database" ) unless $self->in_storage;
my $rows = $self->result_source->storage->update(
- $self->result_source, \%to_update, $self->ident_condition
+ $self->result_source, \%to_update, $self->_storage_ident_condition
);
if ($rows == 0) {
$self->throw_exception( "Can't update ${self}: row not found" );
$self->throw_exception( "Not in database" ) unless $self->in_storage;
$self->result_source->storage->delete(
- $self->result_source, $self->ident_condition
+ $self->result_source, $self->_storage_ident_condition
);
delete $self->{_column_data_in_storage};
$resultset = $resultset->search(undef, $attrs);
}
- return $resultset->find($self->ident_condition);
+ return $resultset->find($self->_storage_ident_condition);
}
=head2 discard_changes ($attrs?)
--- /dev/null
+use strict;
+use warnings;
+
+use Test::More;
+use lib qw(t/lib);
+use DBICTest;
+
+my $schema = DBICTest->init_schema();
+
+my $artist = $schema->resultset('Artist')->next;
+
+is_deeply(
+ [ $artist->id, $artist->ident_condition, $artist->_storage_ident_condition ],
+ [ 1, { artistid => 1 }, { artistid => 1 } ],
+ 'Correct identity state of freshly retrieved object',
+);
+
+$artist->artistid(888);
+
+is_deeply(
+ [ $artist->id, $artist->ident_condition, $artist->_storage_ident_condition ],
+ [ 888, { artistid => 888 }, { artistid => 1 } ],
+ 'Correct identity state of object with modified PK',
+);
+
+$artist->update;
+
+is_deeply(
+ [ $artist->id, $artist->ident_condition, $artist->_storage_ident_condition ],
+ [ 888, { artistid => 888 }, { artistid => 888 } ],
+ 'Correct identity state after storage update',
+);
+
+done_testing;