Railo ORM and Magic Functions

Posted by Mark Drew on code on August 6, 2010

Tagged under railo,getrailo

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.

 

 




comments powered by Disqus