Skip to content

Commit

Permalink
refactor foreign object instantiation into private method
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredbeck committed May 6, 2012
1 parent e61b811 commit a5f7e5c
Showing 1 changed file with 27 additions and 36 deletions.
63 changes: 27 additions & 36 deletions dbrow_renderer.cfc
Expand Up @@ -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>

Expand Down Expand Up @@ -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>

0 comments on commit a5f7e5c

Please sign in to comment.