CFWheels – Move Finders Into Your Model

One of the principles of CFWheels is not duplicating code. This can be achieved a number of ways, but one in particular can also save you some time. Typically our find() methods used to retrieve data have lots of conditions on them, setting the where clause or the order of the returned results, etc. One way to help clean up your controllers and speed up development is by moving those finders into your model to prevent duplication.

Consider the following controller. We are pulling a list of incomplete tasks ordered by their creation date. Now we could possibly have this same exact line of code scattered throughout our application. Anywhere that we would be pulling a list of incomplete tasks.

component name="Tasks" extends="Controller" output="false" {
	public void function index() {
		tasks = model('Task').findAllByComplete(value="false", order="createdAt DESC");

By creating a custom method on our model object, we can clean up the controller code considerably and make it much more readable. Lets look at what the new controller code would look like.

component name="Tasks" extends="Controller" output="false" {
	public void function index() {
		tasks = model('Task').findIncomplete();

Notice how we only need to call the single method now? Short, simple and concise.

Now lets look at how we achieve this by looking at our new Task model.

component extends="Model" output="false" {
	public any function findIncomplete() {
		return this.findAllByComplete(value="false", order="createdAt DESC");

We simply move the same call we were using before into the new method and return the result. We now prefix the finder with this since we are in fact working with an instance of the Task object.

This really helps to reduce duplication of those parameterized finders in your controllers.

7 thoughts on “CFWheels – Move Finders Into Your Model”

  1. Your website is almost impossible to read: blurry super high-contrast white text on a dark background. (windows 7.) It looks like an image that was over-sharpened. (Have you applied a css3 shadow to the text?) I have to use the Readability extension on it.

    Hope it helps.

    1. No there isnt a text-shadow on it. I just checked it out using IE and Chrome on Windows 7 and it looks fine. Not sure why you are having issues with it.

  2. Nice! The only thing I might would do different, just to make the code more decoupled is to exclude the order from the model and still pass that in on a case by case basis. Might not be useful for this case, and it is to early for me to think of some. But I could see this doing so being useful for some cases, maybe 🙂

    Like the ideas overall, and I will definitely be implementing this in my projects!

    1. In order to keep my code as DRY as possible, I wouldnt exclude the order clause. Instead what I typically do is create a method for each way I need it ordered. Typically though, I find one or two ordering methods are usually enough.

      1. Sorry took this long to get back to ya. I completely see what you are saying. Makes perfect sense. Keep up the great posts! Love reading about CFWheels 🙂

    1. I have same issue as your original. I know its over a year ago, but it’s still the same. Have to use the Chrome change colors extension.

Leave a Reply

Your email address will not be published. Required fields are marked *