I have two CFC’s which are Beans with DAO functions. I have set up inheritance where the child cfc extends the parent.
Both objects have identical structures; Functions: init, read, etc. and Properties: ID, etc.
When I create the child object, I pass an ID, which reads the data, getting the foreign key parentID for the parent and then Super.init() is called with appropriate parameters.
My unexpected results are:
Both the ID for the child and the parentID are the same and are the parentID value when the object is returned. The childs variables.ID is overwritten after the super call. I assume that the variables scope is accessible by both, so when parent sets variables.ID it overwrites the child variables.ID. Can this be avoided without naming IDs differently?
The read function of the parent object does not appear to execute. If I rename the parents read function ‘read2’ for example, the function executes. I also suspect that the functions reside in a shared scope and therefore the childs read function is executing.
Is there any way to maintain the same cfc structures and get this functionality to work as expected?
Thanks in advance.
<cfcomponent accessors="true" extends="Custom" output="false">
<cfproperty name="ID" type="numeric" />
<cfproperty name="listID" type="numeric" />
<cfproperty name="customfieldID" type="numeric" />
<cfscript>
variables.dsn = '';
</cfscript>
<cffunction name="init" access="public" output="false" returntype="ListCustom">
<cfargument name="dsn" type="string" required="true" />
<cfargument name="ID" type="numeric" required="true" />
<cfargument name="listID" type="numeric" required="false" default="0" />
<cfargument name="customFieldID" type="numeric" required="false" default="0" />
<cfscript>
variables.dsn = arguments.dsn;
variables.ID = arguments.ID;
variables.listID = arguments.listID;
variables.customFieldID = arguments.customFieldID;
if (variables.ID){
read();
if (variables.customFieldID){
Super.init(dsn=variables.dsn,ID=variables.customfieldID);
}
}
</cfscript>
<cfreturn this />
</cffunction>
<cffunction name="read" access="private" output="false" returntype="void">
<cfquery name="local.q" datasource="#variables.dsn#">
SELECT customfieldID, listID
FROM listCustomFields
WHERE ID = <cfqueryparam value="#variables.ID#" cfsqltype="cf_sql_integer">
</cfquery>
<cfif local.q.recordcount>
<cfset variables.listID = local.q.listID />
<cfset variables.customFieldID = local.q.customFieldID />
</cfif>
</cffunction>
<cfcomponent accessors="true" output="false">
<cfproperty name="ID" type="numeric" />
<cfproperty name="fieldName" type="string" />
<cfscript>
variables.dsn = '';
</cfscript>
<cffunction name="init" access="public" output="false" returntype="Custom">
<cfargument name="dsn" type="string" required="true" />
<cfargument name="ID" type="numeric" required="true" />
<cfargument name="fieldName" type="string" required="false" default="" />
<cfscript>
variables.dsn = arguments.dsn;
variables.ID = arguments.ID;
variables.fieldName = arguments.fieldName;
if (variables.ID){
read();
}
</cfscript>
<cfreturn this />
</cffunction>
<cffunction name="read" access="private" output="false" returntype="void">
<cfquery name="local.q" datasource="#variables.dsn#">
SELECT fieldName
FROM CustomField
WHERE ID = <cfqueryparam value="#variables.ID#" cfsqltype="cf_sql_integer">
</cfquery>
<cfif local.q.recordcount>
<cfset variables.fieldName = local.q.fieldName />
</cfif>
</cffunction>
First, both garygilbert and KRC were helpful in pointing me in the right direction. Thank you.
I gathered the following info:
Both the child and parent will share the same variables scope, so when assigning a value to the variables.ID in super, the child variables.ID value was overwritten.
Similarly, the child function read takes precedence over the parent read function due to OO function overwriting.
My adjusted solution:
Set a temporary ID that is the child id prior to calling Super functions.
Being that if I pass the ID to the init function, the read function is called automatically, I wanted to avoid doing this in the parent object because it would still call the child read.
I call Super.init(dsn) to initialize, then then Super.setID(ID), and finally Super.read(). After the super calls, I restore the initial child ID.