X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FRow.pm;h=e6e5ebc0b266f5738f75b356c78e2d0637988929;hb=2486df86df6c28d8305ae777acdac19ba6faebcf;hp=e03ab16ca551bdd5252edbf5fb436268d45ac7f9;hpb=b8810cc5d0a8888a6a2ec908cdd1c8e9a1522f97;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Row.pm b/lib/DBIx/Class/Row.pm index e03ab16..e6e5ebc 100644 --- a/lib/DBIx/Class/Row.pm +++ b/lib/DBIx/Class/Row.pm @@ -41,6 +41,9 @@ sub new { if ($attrs) { $new->throw_exception("attrs must be a hashref") unless ref($attrs) eq 'HASH'; + if (my $source = delete $attrs->{-result_source}) { + $new->result_source($source); + } foreach my $k (keys %$attrs) { $new->throw_exception("No such column $k on $class") unless $class->has_column($k); @@ -76,6 +79,7 @@ sub insert { $self->in_storage(1); $self->{_dirty_columns} = {}; $self->{related_resultsets} = {}; + undef $self->{_orig_ident}; return $self; } @@ -96,24 +100,29 @@ sub in_storage { =head2 update - $obj->update; + $obj->update \%columns?; Must be run on an object that is already in the database; issues an SQL -UPDATE query to commit any changes to the object to the db if required. +UPDATE query to commit any changes to the object to the database if +required. + +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. =cut sub update { my ($self, $upd) = @_; $self->throw_exception( "Not in database" ) unless $self->in_storage; - $self->set_columns($upd) if $upd; - my %to_update = $self->get_dirty_columns; - return $self unless keys %to_update; my $ident_cond = $self->ident_condition; $self->throw_exception("Cannot safely update a row in a PK-less table") if ! keys %$ident_cond; + $self->set_columns($upd) if $upd; + 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) { @@ -121,6 +130,7 @@ sub update { } $self->{_dirty_columns} = {}; $self->{related_resultsets} = {}; + undef $self->{_orig_ident}; return $self; } @@ -237,6 +247,7 @@ the column is marked as dirty for when you next call $obj->update. sub set_column { my $self = shift; my ($column) = @_; + $self->{_orig_ident} ||= $self->ident_condition; my $old = $self->get_column($column); my $ret = $self->store_column(@_); $self->{_dirty_columns}{$column} = 1 @@ -476,4 +487,3 @@ Matt S. Trout You may distribute this code under the same terms as Perl itself. =cut -