CF9.01 preUpdate fires twice
Since installing the ColdFusion 9.01 updater for ColdFusion 9.0, I've noticed a few quirks (last week I blogged about the behaviour change to cfmailparam). It seems that the ORM event handler now fires the preUpdate method twice when updating a persistent entity. Here is my testcase (using the in-built cfbookclub dsn):
Application.cfc
component
{
this.name = "preupdate_testcase";
this.datasource = "cfbookclub";
this.ormEnabled = true;
this.ormSettings.eventHandling = true;
this.ormSettings.flushatrequestend = false;
}
Author.cfc
component persistent="true" table="Authors"
{
property name="authorid" generator="native" fieldtype="id";
property name="firstname";
property name="lastname";
counter = 0;
void function preUpdate( required struct oldData )
{
counter++;
trace( text="fired preUpdate #counter#", inline=true );
}
}
example.cfm
<cfscript>
transaction
{
Author = EntityLoadByPK( "Author", 1 );
Author.setFirstName( "Mike #TimeFormat( Now(), "MM:SSt")#" );
EntitySave( Author );
}
</cfscript>
When I run this on a ColdFusion 9.01 install, the trace I get is :
[CFTRACE 22:09:44.421] [31 ms] [C:\htdocs\Aliaspooryorik\samples\preUpdate\Author.cfc @ line: 17] - fired preUpdate 1
[CFTRACE 22:09:44.421] [31 ms] [C:\htdocs\Aliaspooryorik\samples\preUpdate\Author.cfc @ line: 17] - fired preUpdate 2
Running this on a ColdFusion 9.00 install, the preUpdate only fires once (as I would expect).
For me this was a real pain to track down as I use the preUpdate implicit method to encrypt passwords, so the password was being encrypted and then the encrypted password was encrypted again!
As a work around I've had to do something like this:
User.cfc
component persistent="true" table="Users"
{
property name="userid" generator="native" fieldtype="id";
property name="username";
property name="password";
property name="salt";
variables.preUpdateFired = false;
/*
* Implicit Methods
* -------------------------------------------------------------------- */
void function preInsert()
{
encryptPassword();
}
void function preUpdate( required struct oldData )
{
// check this hasn't already executed
if ( !variables.preUpdateFired )
{
// only encrypt password if password has changed
if ( arguments.oldData.password != variables.password )
{
encryptPassword();
}
variables.preUpdateFired = true;
}
}
/*
* Private Methods
* -------------------------------------------------------------------- */
private void function encryptPassword()
{
var counter = 0;
// hash multiple times to prevent the use of rainbow tables
while ( counter < 50 )
{
variables.password = Hash( variables.salt & variables.password, "SHA-512" );
counter++;
}
}
}
- Posted in:
- ColdFusion


Comment by Sami Hoda – August 02, 2010
cfbugs.adobe.com/cfbugreport/flexbugui/cfbugtracker/main.html
Comment by Henry Ho – August 03, 2010
Yes, I have now filed a bug report for it.
cfbugs.adobe.com/cfbugreport/flexbugui/cfbugtracker/main.html#bugId=83748
Comment by John Whish – August 03, 2010
kb2.adobe.com/cps/862/cpsid_86263.html
Comment by John Whish – September 01, 2010