Subject: | obvious way to change columns in before_create / before_update doesn't work |
I have a Class::DBI table where I want to keep track, for each row, of its creation and modification date. My first thought was to implement this using before_create and before_update triggers. At first this seemed to work, but when I tried using the classes in Maypole I experienced unexpected infinite loop hangs, due to its turning on autoupdate. It turns out that if you set a column in one of the accessor methods within a before_update trigger, and autoupdate is on, it causes an infinite loop. Within a before_create trigger it just seems to not work at all, although my experiences have been mixed here and I'm not sure of the cause. At any rate, from perusing the mailing list I came across the following solution:
sub before_create {
my $self = shift;
my $t = gmtime;
$self->_attribute_set(c_time => $t);
$self->_attribute_set(m_time => $t);
}
sub before_update {
my $self = shift;
my $t = gmtime;
$self->_attribute_set(m_time => $t);
}
(Where c_time and m_time are inflated to Time::Piece and deflated to appropriate strings.)
So, while it's possible for me to do what I want, there's nothing in the Class::DBI documentation to tell me how to do it or what the pitfalls might be. At any rate, it would be nice if there were a standard way to accomplish this. For it to just work with the accessors might be too much to ask, as I realize the various difficulties, but these issues should at least be documented! That is, what you can and cannot do within a trigger of a given type.