Flow Differences between .cfm and cffunction in a .cfc?
I need to manage a list of courses that exist in a database, but for various (good) reasons I can't store extra data in the primary database. So I wrote up a relatively simple cfm page that had the following flow:
(first, make sure the lists are identical)
cfquery (*, mainCourseDb)
cfloop
for each courseID
cfquery (*, myListDb where courseID is mainCourseDb.courseID)
if recordcount IS 0
cfquery(insert mainCourseDb.* into myListDb.*)
/cfloop
(then, show the user their list)
cfquery (*, where user is the current user)
When I do this in a cfm, everything works fine. But when I converted this to a cfc inside of a cffunction, only the last part works.
Can anyone explain why the first block of code doesn't execute at all when it's in a cffunction or cfc?
I'll add the actual code incase that helps (I changed some names to obscure actual datasources, table names, etc... also, I'm using cflog to trace what's executing), to reiterate this works if I pull the code in the cffunction out and just stick it in a cfm, but in a cfc (or in a cffunction) only the last query executes ... the cfloop stuff at the top doesn't happen:
<cfcomponent> <cffunction name="getCourseList" access="remote" returntype="string" returnformat="json"> <cfargument name="loginname" type="string" required="yes"> <cfset var sendCourseList = "" > <cfquery name="getCourseList" datasource="courses"> SELECT * FROM dbo.mainCourseDb WHERE loginname = '#ARGUMENTS.loginname#%' ORDER BY courseNum ASC </cfquery> <cflog file="migrationList" application="no" text="User #ARGUMENTS.loginname# requested a list."> <!--- loop through user's courses to compare course IDs (courseNum) to migration list ---> <cfloop query="getCourseList"> <cflog file="migrationList" application="no" text="User #ARGUMENTS.loginname# began the getCourseList cfloop."> <cfquery name="getMigrationList" datasource="myListDb" result="tmpResult"> SELECT * FROM migrationTrack WHERE courseID = <cfqueryparam value="#getCourseList.courseNum#"> AND username = '#ARGUMENTS.loginname#%' ORDER BY courseID ASC </cfquery> <cfif tmpResult.recordcount IS 0 > <cflog file="migrationList" application="no" text="User #ARGUMENTS.loginname# : course #getCourseList.COURSE_NAME# not found, adding via try "> <cftry> <cfquery name="addToMigrationList" datasource="mycourseList" result="courseAdded"> INSERT INTO migrationTrack (courseID, username, courseName, status) VALUES( '#getCourseList.courseNum#', '#getCourseList.loginname#', '#getCourseList.COURSE_NAME#' , '0' ) </cfquery> <cflog file="migrationList" application="no" text="User #ARGUMENTS.loginname# : course #getCourseList.COURSE_NAME# added "> <cfcatch type=”any”> <cflog file="migrationList" application="no" text="User #ARGUMENTS.loginname# : <strong></strong> course #getCourseList.COURSE_NAME# not added - An error of some kind occured; check logs? "> </cfcatch> </cftry> </cfif> </cfloop> <!--- only the code below executes when put in a cfc / cffunction ---> <cfquery name="sendCourseList" datasource="mycourseList"> SELECT courseID, courseName, status, isMigrated, isQueued, copyOnly FROM migrationTrack WHERE username = <cfqueryparam value="#Arguments.loginname#" cfsqltype="cf_sql_clob" maxlength="255"> ORDER BY status, courseName ASC </cfquery> <cfreturn serializeJSON(sendCourseList)> </cffunction> </cfcomponent>
