我有两个 CFC,它们是具有 DAO 功能的 Bean。我已经在子 cfc 扩展父的地方设置了继承。
两个对象具有相同的结构;函数:init、read 等和 Properties:ID 等。
当我创建子对象时,我传递了一个 ID,它读取数据,获取父对象的外键 parentID,然后使用适当的参数调用 Super.init()。
我出乎意料的结果是:child 和 parentID 的 ID 相同,并且是返回对象时的 parentID 值。childs variables.ID 在 super 调用后被覆盖。我假设两者都可以访问变量范围,因此当父级设置 variables.ID 时,它会覆盖子 variables.ID。如果不以不同的方式命名 ID,是否可以避免这种情况?
父对象的读取函数似乎没有执行。例如,如果我重命名父母读取函数“read2”,该函数就会执行。我还怀疑这些函数位于共享范围内,因此正在执行子读取函数。
有什么方法可以保持相同的 cfc 结构并使此功能按预期工作?
提前致谢。
<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>