trigger documentation bug?

[prev] [thread] [next] [Date index for 2004/11/28]

From: Ryan Tate
Subject: trigger documentation bug?
Date: 23:38 on 28 Nov 2004
Good autumnal eve,

It appears as though the documentation for Class::DBI triggers may be 
incorrect when it states "all triggers are passed the object they are 
being fired for" in the particular case of "before_set_$column" when 
"before_set_$column" is fired during ->create.

The Class::DBI perldoc's "Triggers" section states:

"All triggers are passed the object they are being fired for. Some 
triggers are also passed extra parameters as name-value pairs. The individual 
triggers are documented with the methods that trigger them."

The documentation for ->create states further:


"The before_create trigger is invoked directly after storing the supplied 
values into the new object and before inserting the record into the 
database. The object stored in $self may not have all the functionality of 
the final object after_creation, particularly if the database is going to 
be providing the primary key value."

But it appears the trigger "before_set_$column" is called during object 
creation, prior to the "before_create" trigger. When fired during 
creation, "before_set_$column" is not passed the object it is being fired 
for, as stated in the documentation. Rather, it is passed the *class* it 
is being fired for.

Here is my test script, followed by the output:

----------

use strict;

use lib '/services/http/users/r/ryantate/cgi-bin/lib';

use Class::DBI;

package Dashboard::Stored;
use  base 'Class::DBI';
__PACKAGE__->connection('DBI:mysql:database=ryantate;host=death.ocf', 
'ryantate', 'notmypassword');

package Dashboard::Note;
use base 'Dashboard::Stored';
use Data::Dumper;
__PACKAGE__->table('dashboard_note');
__PACKAGE__->columns(All => qw/id headline body created updated/);

Dashboard::Note->add_trigger( before_set_headline => \&check_user_data);

print my $note2 = Dashboard::Note->create({headline=>'test of class::dbi',
body=>'this is a test.', created=>'2004-11-28 15:11:00'}), "\n";


  sub check_user_data{
print "DEBUG " . Dumper($_) . "\n" foreach @_;
  }


----------

apocalypse.OCF.Berkeley.EDU [210] perl5.6.1 -w cdbi_doc_bug_case
DEBUG $VAR1 = 'Dashboard::Note';

DEBUG $VAR1 = 'test of class::dbi';

DEBUG $VAR1 = {
           'headline' => 'test of class::dbi',
           'body' => 'this is a test.',
           'created' => '2004-11-28 15:11:00'
         };

148
apocalypse.OCF.Berkeley.EDU [211]

----------


I hit this doc bug because the callback for my "before_set_$column" 
trigger needed to look up some info based on the class of the object it 
was fired for. I used "my $class = ref $self;" to determine the class; 
this worked fine until the trigger was fired for an object create. The fix 
is easy but only because I now know the actual trigger behavior of 
Class::DBI in this case,

If there is anything I can do to help patch the docs, I'd be happy to! 
(... Assuming I am correct that this is a bug in the docs.)

Cheers
RT

trigger documentation bug?
Ryan Tate 23:38 on 28 Nov 2004

Re: trigger documentation bug?
Tim Bunce 09:33 on 29 Nov 2004

patch Re: trigger documentation bug?
Ryan Tate 22:07 on 22 Dec 2004

Re: patch Re: trigger documentation bug?
Tim Bunce 22:34 on 22 Dec 2004

Generated at 12:15 on 16 Jan 2005 by mariachi v0.52