[CDBI] Re: HOW TO populate creation date automatically?

Rhesa Rozendaal perl at rhesa.com
Mon Jan 9 00:42:46 GMT 2006


Vitaliy Babiy wrote:
> Hey
> 
> 
>>In general, you don't want to use set() or get() from inside a trigger, since
>>they invoke triggers themselves. _attribute_store() is the correct method to
>>use here. Refer to the documentation for evidence:
>>
>>http://search.cpan.org/dist/Class-DBI/lib/Class/DBI.pm#LOW-LEVEL_DATA_ACCESS

Edward: thanks for that link. However, the docs say something slightly different:

_attribute_store [...]
     This only updates values in the object itself; changes will not be
     propagated to the database.

_attribute_set [...]
     Updates values in the object via _attribute_store(), but also logs the
     changes so that they are propagated to the database with the next update.
     (Unlike set(), however, _attribute_set() will not trigger an update if
     autoupdate is turned on.)


> This exactly what I'm doing. But I can not understand why it's not
> working. Am I missing something?
> 
> 
> __PACKAGE__->add_trigger( before_create  => 
>      sub { my $self = shift;                         
>            $self->_attribute_store( { 
>                    dateCreated => Class::Date->now } );
>          } );

Vitaliy: in other words, the docs say that you should be using _attribute_set 
here.

My original suggestion using set() works because I only call set() if the 
column doesn't yet have a value, but using _attribute_set looks like an even 
better solution. I'll test that one myself soon.

HTH,
Rhesa




More information about the ClassDBI mailing list