From: Brandon L. Black Date: Mon, 11 Dec 2006 01:00:22 +0000 (+0000) Subject: allow pk mutation via column accessor + update X-Git-Tag: v0.08010~205 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6c299e8b164f858bccdb635df9adeffc23b2b136;p=dbsrgits%2FDBIx-Class.git allow pk mutation via column accessor + update --- diff --git a/lib/DBIx/Class/Row.pm b/lib/DBIx/Class/Row.pm index 213f3cc..ee4e84c 100644 --- a/lib/DBIx/Class/Row.pm +++ b/lib/DBIx/Class/Row.pm @@ -117,7 +117,7 @@ sub update { my %to_update = $self->get_dirty_columns; return $self unless keys %to_update; my $rows = $self->result_source->storage->update( - $self->result_source->from, \%to_update, $ident_cond); + $self->result_source->from, \%to_update, $self->{_orig_ident} || $ident_cond); if ($rows == 0) { $self->throw_exception( "Can't update ${self}: row not found" ); } elsif ($rows > 1) { @@ -242,6 +242,18 @@ sub set_column { my $self = shift; my ($column) = @_; my $old = $self->get_column($column); + + # save our original ident condition if + # they modify any part of the PK + if(!$self->{_orig_ident}) { + foreach ($self->primary_columns) { + if($_ eq $column) { + $self->{_orig_ident} = $self->ident_condition; + last; + } + } + } + my $ret = $self->store_column(@_); $self->{_dirty_columns}{$column} = 1 if (defined $old ^ defined $ret) || (defined $old && $old ne $ret); diff --git a/t/69update.t b/t/69update.t index b11ebde..4686876 100644 --- a/t/69update.t +++ b/t/69update.t @@ -9,7 +9,7 @@ my $schema = DBICTest->init_schema(); BEGIN { eval "use DBD::SQLite"; - plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 5); + plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 6); } my $art = $schema->resultset("Artist")->find(1); @@ -30,3 +30,7 @@ $art->discard_changes; ok($art->update({ artistid => 100 }), 'update allows pk mutation'); is($art->artistid, 100, 'pk mutation applied'); + +my $art_100 = $schema->resultset("Artist")->find(100); +$art_100->artistid(101); +ok($art_100->update(), 'update allows pk mutation via column accessor');