Advertisement
eldieck

ColdFusion UserComponent Example

Apr 21st, 2017
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <cfcomponent displayname="User">
  2.    
  3.   <cffunction name="getUser" access="public" returntype="query" output="0">
  4.     <cfargument name="userId" type="numeric" required="yes" default="0">
  5.     <cfset var thisUserId = trim(arguments.userId)>
  6.     <cfif isValid("integer",thisUserId)>
  7.         <cfquery name="qryGetUser" datasource="#application.DSN#">
  8.             SELECT * FROM tblUsers
  9.             WHERE user_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#thisUserId#">
  10.         </cfquery>
  11.     <cfelse>
  12.         <--- Return an empty query if the argument is not an integer --">
  13.         <cfquery name="qryGetUser" datasource="#application.DSN#">
  14.             SELECT * FROM tblUsers WHERE user_id = 0
  15.         </cfquery> 
  16.     </cfif>
  17.      <cfreturn qryGetUser>
  18.   </cffunction>
  19.    
  20.   <cffunction name="updateUser" access="public" returntype="boolean" output="0">
  21.     <cfargument name="userStructure" type="struct" required="yes">
  22.     <cfscript>  
  23.       valueSet = "";
  24.       for (key in myStruct) {
  25.         if(myStruct[key] NEQ ""){
  26.           valueSet = listAppend(valueSet,key & "='" & utilityObj.sqlSafe(myStruct[key]) & "'");
  27.         }else{
  28.           valueSet = listAppend(valueSet,key & "='" & myStruct[key] & "'");
  29.         }
  30.       }
  31.     </cfscript>
  32.     <cftry>
  33.       <cfstoredproc datasource="#application.DSN#" procedure="dbo.sp_update_user">
  34.       <cfprocparam value="#arguments.userId#" cfsqltype="cf_sql_varchar">
  35.       <cfprocparam value="#valueSet#" cfsqltype="cf_sql_varchar">
  36.       <cfprocresult name="qA">
  37.       </cfstoredproc>
  38.       <cfcatch type="database">
  39.         <cfoutput>#cfcatch.detail#</cfoutput><cfabort>
  40.       </cfcatch>
  41.     </cftry>
  42.      <cfreturn>
  43.   </cffunction>
  44. </cfcomponent>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement