[CDBI] Updating a record - slight change proposal
[prev]
[thread]
[next]
[Date index for 2005/09/07]
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?
_______________________________________________
ClassDBI mailing list
ClassDBI@xxxxx.xxxxxxxxxxxxxxxx.xxx
http://lists.digitalcraftsmen.net/mailman/listinfo/classdbi
|
[CDBI] Updating a record - slight change proposal
Kate Yoak 01:53 on 07 Sep 2005
|