top of page

Defining Functions in CFWheels

Recently, when working with a developer that was new to cfwheels, I was asked, "Where is the best place to put functions that manipulate data?" Like a good consultant I answered the question with a question - "What do you mean by 'manipulate data'," I asked. Really the answer lies somewhere in the "classic" separation of concerns which in MVC architecture is pretty clear - are we talking manipulation for visual reasons, business logic reasons or persistence? Or perhaps, multiple reasons? there's your answer.

So, using an example, lets say we are working with an employee record (table-name: employee) (yes, I break the convention and use singluar table names cfwheels people, bite me, it's my app, i like it more. A nice feature of wheels is that you can break conventions and configure when you want or need to.) Anyway, Let's assume we need to manipulate some data. where do we put the function?

Example 1: encrypt the data for a field on the way into or out of the database table

Solution 1: put it in the model. the concern is persistence. Unless you are going to be

displaying or otherwise "controlling" the encrypted values somewhere else in the app (not likely) then the other concerns of our architecture dont need to know about this functionality - so don't let them even see it: Thus, in /models/employee.cfc add

<cffunction name="encryptPhone" returntype="void" hint="I'll encrypt the phone field"> <cfset = encrypt(,"somekey") /> </cffunction>

Note: If this is a generic function that will be needed in many models (or can be used in many models, say, something like a function to generate an audit trail record), then add this code to the ancestor class /models/model.cfc. Then, all of your models will already have this functionality wired in AND it will still be isolated to our persistence concern

Example 2: manipulate form data for validation or other conversion of request parameters to something structurmafied (yes, that's my own word)

Solution 2: put it in the controller

bottom of page