[CDBI] Updating a record - slight change proposal

Kate Yoak kate at yoak.com
Wed Sep 7 02:53:14 BST 2005


Here is the way one might update an object:

my $obj = Class->retrieve($id);
$obj->set(%all_the_data);
$obj->update;


There is a slight problem here - the object you get back will be entirely
empty if $id is contained within %all_the_data.  The id field gets discarded
because it has been marked as changed.

Here is a change I made in my cdbi class (I am overriding _attribute_set):

sub _attribute_set {
   my $self = shift;
   my $vals = @_ == 1 ? shift: {@_};

   # We increment instead of setting to 1 because it might be useful to
   # someone to know how many times a value has changed between updates.

   for my $col (keys %$vals) {
      #### ADDING  the unless statement here:
      #$self->{__Changed}{$col}++ ;
      $self->{__Changed}{$col}++ unless $self->_attr($col) eq $vals->{$col};
   }
   $self->_attribute_store($vals);
}

In other words, don't mark something as changed if it hasn't really changed.

What did I break?








More information about the ClassDBI mailing list