[CDBI] HOW TO populate creation date automatically?

Rhesa Rozendaal perl at rhesa.com
Sun Jan 1 00:37:24 GMT 2006


Vitaliy Babiy wrote:
> Hi 
> 
> I have table with column 'dateCreated' which supposed to have date when
> record was created. How can I populate that field automatically with
> CDBI?
> 
> I tried to use trigger 'before_create' to populate that field, but it
> doesn't work. What I'm missing here?
> 
> __PACKAGE__->add_trigger(before_create  => 
> sub { my $self = shift;                         
>       $self->_attribute_store( { dateCreated => Class::Date->now } );
>       } );


Why do you use _attribute_store here, which looks like an internal method?
I use set() as follows in my base class, and it works fine for every table 
with a create_date column:

__PACKAGE__->add_trigger(before_create => sub {
     $_[0]->set(create_date => DateTime->now)
         if       $_[0]->find_column('create_date')
            and ! $_[0]->get('create_date')
     });

HTH,
Rhesa




More information about the ClassDBI mailing list