[CDBI] after_set_$col and after_create

Kate Yoak kate at longmedia.com
Tue Oct 25 04:07:49 BST 2005

I keep running into this inconsistency in using triggers and was wondering
if there is a standard way to get around it.

The statemtnt

my $foo=Foo->create({first=>"hello", second=>"world"});

is basically equivalent to

my $foo = Foo->create();
$foo->set(first=>"hello", second=>"world");

with the exception of some performance considerations.  In general, we
chosse one form over the other depending on the desired flow of execution,
efficiency, etc.

Here is a type of trigger that one might write if real triggers are not
available within the db engine:

Foo->add_trigger(after_set_first => sub {shift()->flag(1) });

This trigger will only be executed in the second example above - when we are
using set() to place the values into the record, rather than create -
Class::DBI::create() does not use set, nor calls after_set_ triggers.

To fix this, we may add a before_create trigger that checks if we are
setting the column and calls the same function.

Now everything works as it should.  However, the default behavior seems off
as the developer has to make sure all the after_set (or before_set) triggers
are duplicated elsewhere.

More information about the ClassDBI mailing list