created in the spirit of Ruby on Rails.
Make your model calls more readable by using dynamic finders.
With the recent introduction of onMissingMethod() in ColdFusion 8 (Thanks, Adobe!), we have been able to port over the concept of dynamic finders from Rails to Wheels.
The concept is simple. Instead of using arguments to tell Wheels what you want to do, you can use a dynamically-named function.
For example, the following code:
<cfset me = model("User").findOne(where="email='me@myself.com'")>
Can also be written as:
<cfset me = model("User").findOneByEmail("me@myself.com")>
Through the power of onMissingMethod(), Wheels will parse the function name and figure out that the value supplied is supposed to be matched against the email column.
You can take this one step further by using code such as:
<cfset me = model("User").findOneByUserNameAndPassword("perd,pass")>
In this case, Wheels will split the function name on the And part and determine that you want to find the record where the username column is "perd" and the password column is "pass".
In the examples above, we've used the findOne() function, but you can use the same approach on a findAll() function as well.
Keep in mind that this will break down if you ever name a column somethingAndSomethingElse() because Wheels will then split the function name incorrectly.
Comments
Read and submit questions, clarifications, and corrections about this chapter.
There are no comments for this chapter. Be the first to comment!