Aliaspooryorik
ColdFusion ORM Book

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++;
}
}
}

 


4 comments

  1. Is there a bug for this?

    Comment by Sami Hoda – August 02, 2010
  2. Have you filed the bug?

    cfbugs.adobe.com/cfbugreport/flexbugui/cfbugtracker/main.html

    Comment by Henry Ho – August 03, 2010
  3. Hi Sami and Henry,

    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
  4. This has now been patched in Cumulative Hotfix 1 (CHF1) for ColdFusion 9.0.1:
    kb2.adobe.com/cps/862/cpsid_86263.html

    Comment by John Whish – September 01, 2010

Leave a comment

If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)

Please note: If you haven't commented before, then your comments will be moderated before they are displayed.

Please subscribe me to any further comments
 

Search

Wish List

Found something helpful & want to say ’thanks‘? Then visit my Amazon Wish List :)

Categories

Recent Posts