Mark Drew (Redux)- cf_etc...

a compendium of railo, cfml, cfeclipse and technology topics

Mark Drew (Redux)- cf_etc...

Railo ORM and Magic Functions

August 6, 2010 · 4 Comments

Background: I am writing a little app to help us here at Railo with some of our contacts, nothing over the top or anything and wanted to take Sean Corfield's FW/1 for a spin.

As I am developing, I went the most direct route, I just did some queries in the service layer so my code looks like:

<cffunction name="list" output="false">
	<cfset var clients = 0>
	
	<cfquery name="clients" datasource="#variables.dsn#">
		SELECT * FROM contact;
	</cfquery>
	<cfreturn clients>
</cffunction>


<cffunction name="view" output="false">
	<cfargument name="clientid">
	<cfset var qclient = 0>
	<cfquery name="qclient" datasource="#variables.dsn#">
		SELECT * FROM contact WHERE id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.clientid#">;
	</cfquery>
	<cfreturn qclient>
</cffunction>

OK, nothing world shattering, so I wanted to start using the ORM features of Railo (join the pre-release group to test them out!). I changed my code to look like:

<cffunction name="list" output="false">
	<cfset var clients = 0>
	
	<cfquery name="clients" dbtype="orm">
		FROM contact
	</cfquery>
	<cfreturn clients>
</cffunction>


<cffunction name="view" output="false">
	<cfargument name="id">
	<cfreturn entityLoad("contact",arguments.id,true)>
</cffunction>

I then defined my "contact" (I can show the code but it's hardly rocket science) and then when I reloaded I got the error:

Component [contact] has no acessible Member with name [ID]

Of course! My view code looks like:

<input type="hidden" name="id" value="#rc.data.id#">


I could change all my calls to fields to

<input type="hidden" name="id" value="#rc.data.getid()#">


but why bother? I just went to the Railo Admin -> Archives & Resources: Component -> Magic Functions and enabled them, now all my view code works just as expected and I didn't have to change my view code.

 

 

Tags: getrailo · railo

4 responses so far ↓

  • 1 Tony Garcia // Aug 6, 2010 at 5:59 PM

    That's pretty cool. At first, I wasn't too crazy about Railo's magic functions because the syntax made it feel like I was dealing with structs or accessing public attributes. But as I've started looking at other languages I've found that it's more in line with how they implement implicit getters in setters (as opposed to the ACF9 way) and I'm warming up to it.

    check out:
    http://eli.thegreenplace.net/2009/02/06/getters-and-setters-in-python/
  • 2 Laura // Aug 6, 2010 at 8:59 PM

    That is exactly how it is done in ActionScript as well.
  • 3 John Farrar // Aug 6, 2010 at 11:38 PM

    That is very cool but couldn't you have also done an entityToQuery() call?
  • 4 Mark Drew // Aug 9, 2010 at 11:04 AM

    @John, I guess I could have converted it to a Query but what impressed me is that I can have a clean service layer as well as not touching my view layer.

Leave a Comment

Leave this field empty: