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

[prev] [thread] [next] [Date index for 2006/01/09]

From: Rhesa Rozendaal
Subject: Re: [CDBI] Re: HOW TO populate creation date automatically?
Date: 00:42 on 09 Jan 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

_______________________________________________
ClassDBI mailing list
ClassDBI@xxxxx.xxxxxxxxxxxxxxxx.xxx
http://lists.digitalcraftsmen.net/mailman/listinfo/classdbi

(message missing)

[CDBI] Simple join
Jon Molin 00:16 on 30 Dec 2005

Re: [CDBI] Simple join
Peter Speltz 20:13 on 30 Dec 2005

Re: [CDBI] Re: HOW TO populate creation date automatically?
Rhesa Rozendaal 00:42 on 09 Jan 2006

Generated at 01:21 on 10 Jan 2006 by mariachi v0.52