[CDBI] Using before_update trigger to history

Peter Speltz peterspeltz at gmail.com
Sat Jan 21 03:13:51 GMT 2006


On 1/20/06, Peter Speltz <peterspeltz at gmail.com> wrote:
>
> #Then the update trigger would be as simple as
> __PACKAGE__->add_trigger( before_update => sub {
>      my $self        = shift;
>      my $dump = join ( ', ', map { $_ . ", ". $self->$_ } $self->columns );
>      $self->add_to_history(dump => $dump);

But code like above no work :( .  2 main problems -. Here is the code
I am using :

# History of this table
__PACKAGE__->has_many(history => __PACKAGE__->_namespace_.'::History' ,
        'row_id' , {constraint =>{table_name => __PACKAGE__->table}}); ## WORKS

# update trigger would be as simple as
__PACKAGE__->add_trigger( before_update => sub {
        my $self        = shift;
        $self->add_to_history({dump => $self->_dump_data_});
    });

# Returns parseable string of data from table
sub _dump_data_ {
    my $self = shift;
    die "Object Method Only!" unless ref $self;
    my $dump =join ( ', ', map { $_ . ", ". $self->_attr($_) } $self->columns );
    return $dump;
}

Good news is the has_many , add_to is working as expected.

Problems with above :

1)   Getting new data when calling $self->_attr($_) rather than the
old data I want to record in history. I did not expect $self to
contain new data in a before update trigger.

Thinking about it though, the before set trigger is probably where the
old data would be found in $self and simplest thing would be to just
do a select out of DB.   This will solve 2)

2)  Despite using low level accessor _attr, I 'm still getting
stringifed objects in $dump. I do not understand this .

Back to it.
--
pjs




More information about the ClassDBI mailing list