Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactor foreign object instantiation into private method

  • Loading branch information...
commit a5f7e5c770e80689b4515e6f86b60de34dc1f839 1 parent e61b811
Jared Beck jaredbeck authored
Showing with 27 additions and 36 deletions.
  1. +27 −36 dbrow_renderer.cfc
63 dbrow_renderer.cfc
View
@@ -15,46 +15,14 @@ public component function init(required component dbrowObj) {
<cfscript>
var formField = "";
var stMD = this.dbrowObj.stColMetaData[arguments.propertyname];
- var foreignColumn = "";
- var foreignTable = "";
- var foreignNameColumn = "";
- var foreignObjPath = "";
- var objForeign = "";
var isForeignKey = structKeyExists(this.dbrowObj.stFKMetaData, arguments.propertyname);
</cfscript>
<cfif isForeignKey>
- <cfset foreignColumn = this.dbrowObj.stFKMetaData[arguments.propertyname].foreignColumn>
- <cfset foreignTable = this.dbrowObj.stFKMetaData[arguments.propertyname].foreignTable>
-
- <!--- BEGIN - Figure out the name column in the foreign table and the right path for instantiating the foreign object - leon 12/13/07 --->
- <!--- First try to get the name column from the mapper - leon 12/13/07 --->
- <cfif structKeyExists(application, 'dbrow3mapper')>
- <cfset foreignObjPath = application.dbrow3mapper.getObjPathFromTable(foreignTable)>
- <cfif len(foreignObjPath)>
- <cfset foreignNameColumn = application.dbrow3mapper.getNameColFromObjPath(foreignObjPath)>
- </cfif>
- </cfif>
- <!--- If we couldn't get the name column from the mapper, guess at the foreign object name and try to get it from there - leon 12/13/07 --->
- <cfif not(len(foreignNameColumn))>
- <cfset objForeign = createObject('component', '#this.dbrowObj.objectMap#.#lcase(REReplaceNoCase(foreignColumn, '_?ID$', ''))#').init()>
- <cfset foreignNameColumn = objForeign.theNameField>
- </cfif>
- <cfif not(len(foreignNameColumn))>
- <cfthrow message="Could not determine name column in foreign key table.">
- </cfif>
-
- <cfif not(len(foreignObjPath))>
- <!--- We didn't get a foreignObjPath from the mapper, so guess at it. - leon 12/13/07 --->
- <cfset foreignObjPath = this.dbrowObj.objectMap & "." & REReplaceNoCase(foreignColumn, '_?id$', '')>
- </cfif>
- <!--- END - Figure out the name column in the foreign table and the right path for instantiating the foreign object - leon 12/13/07 --->
-
- <cfset v.objForeign = createObject('component', '#foreignObjPath#').new()>
-
- <cfset v.objForeign.load( this.dbrowObj[arguments.propertyname] ) >
-
- <cfset formField = evaluate( "v.objForeign.#foreignNameColumn#" ) >
+ <cfset var objForeign = getOneAssociatedModel(arguments.propertyname)>
+ <cfset objForeign.load( this.dbrowObj[arguments.propertyname] )>
+ <cfset var foreignNameColumn = objForeign.theNameField>
+ <cfset formField = objForeign[foreignNameColumn]>
<cfelse>
@@ -90,4 +58,27 @@ public component function init(required component dbrowObj) {
<cfreturn label>
</cffunction>
+
+<cffunction name="getOneAssociatedModel" returntype="component"
+ access="private" output="no" hint="Given the name of a foreign
+ key column, return the associated model instance.">
+ <cfargument name="propertyname" type="string" required="yes">
+
+ <cfset var foreignObjPath = "">
+
+ <!--- First try to get it from the mapper - leon 12/13/07 --->
+ <cfif StructKeyExists(application, 'dbrow3mapper')>
+ <cfset var foreignTable = this.dbrowObj.stFKMetaData[arguments.propertyname].foreignTable>
+ <cfset foreignObjPath = application.dbrow3mapper.getObjPathFromTable(foreignTable)>
+ </cfif>
+
+ <!--- If we didn't get a foreignObjPath from the mapper, guess at it. - leon 12/13/07 --->
+ <cfif NOT Len(foreignObjPath)>
+ <cfset var foreignColumn = this.dbrowObj.stFKMetaData[arguments.propertyname].foreignColumn>
+ <cfset foreignObjPath = this.dbrowObj.objectMap & "." & REReplaceNoCase(foreignColumn, '_?id$', '')>
+ </cfif>
+
+ <cfreturn CreateObject('component', '#foreignObjPath#').new()>
+</cffunction>
+
</cfcomponent>
Please sign in to comment.
Something went wrong with that request. Please try again.