Is there a way to see if a field has changed inside a model on update?

44 views
Skip to first unread message

Amir

unread,
Apr 18, 2013, 2:40:05 PM4/18/13
to cfwh...@googlegroups.com
Basically I'm trying to set a function that will fire on model update IF a value has changed.

For example: An author has many books, and each book has an AUTHORID and CATEGORYID. I want something to happen if that author changes the CATEGORYID.

So, if he updates a book, and changes the category, I want to fire a function that will e-mail me; notifying me of the change.

My second problem is that I think I will have to put this e-mail function inside my model. Because in the controller I just call author.update(params.author), and the author could update many books at the same time. So I need something that will fire for each book. Does that make sense?

Now I'm pretty sure I can put all this logic inside model with <cfset beforeUpdate(method="notifymeofchange")>, but I am not sure how I can detect a value change on CATEGORYID. Any ideas?

Thanks!

Simon Allard

unread,
Apr 18, 2013, 3:28:45 PM4/18/13
to cfwh...@googlegroups.com
Hi Amir,

You may want to check the allChanges() function. I did something similar in one of my apps where I wanted to log all changes made to a model. This sample script may give you a start.

Model file (someModel.cfc)

<cffunction name="init">
  <cfset beforeUpdate("trackChanges")>
</cffunction>

<cffunction name="trackChanges">
    <cfset var loc = {}>
    <cfset loc.audit = this.allChanges()>    
    <cfset this.audit = loc.audit>
  </cffunction>

Controller file 

<cffunction name="yourUpdateFunction">
  <cfset var loc = {}>
  <cfset loc.update = arguments.form>

  <cfif someModel.update(loc.update)>

    <cfset loc.audit = someModel.audit>

    <cfloop collection="#loc.audit#" item="loc.key">

      <cfif loc.key EQ "categoryid">

         <cfset loc.changedfrom = loc.audit[loc.key]["CHANGEDFROM"]>
         <cfset loc.changedto = loc.audit[loc.key]["CHANGEDTO"]>

         <cfif loc.changedfrom NEQ loc.changedto>
              <cfset sendYourEmail()>
         </cfif>

      </cfif>

    </cfloop>

  </cfif>

</cffunction>




--
You received this message because you are subscribed to the Google Groups "ColdFusion on Wheels" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cfwheels+u...@googlegroups.com.
To post to this group, send email to cfwh...@googlegroups.com.
Visit this group at http://groups.google.com/group/cfwheels?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Amir

unread,
Apr 18, 2013, 4:09:07 PM4/18/13
to cfwh...@googlegroups.com
This is interesting, thank you for sharing! If I'm reading that code correctly, it will fire only once per update() call, right? So it probably wouldn't work for me in the Controller, but perhaps in the model file directly. I hate to have to put logic code in there, but it seems to me like the only option since I'm updating multiple rows at once.

What do you think?

Andy Bellenie

unread,
Apr 18, 2013, 4:09:12 PM4/18/13
to ColdFusion on Wheels
Use hasChanged to detect a value change inside a model

Sending an email is a controller action however, so I would set a property on the model to alert the controller to send the email.

Amir

unread,
Apr 18, 2013, 4:20:45 PM4/18/13
to cfwh...@googlegroups.com
Awesome! Thanks Andy!

Any advice on the e-mail code inside the model? Is there any way to detect that change, and then offload the email code to a controller?

Ideally I'd have the Update() method run, and then once it's done updating, hand it off back to the controller, to e-mail me the change, rather than interjecting the e-mail process into the update() call. Does that make sense?
Reply all
Reply to author
Forward
0 new messages