Just wanted to throw in again the nice tip that someone mentioned here to
use <cfset var ...> (I believe that such declarations need to happen at the
top of the method):
<cffunction name="getDescription">
<cfargument name="item" required="yes" type="numeric" />
<cfset var local = structNew() />
<cfset local.detailQuery = getDetail( arguments.item ) />
<cfset local.desc = "" />
<cfif len(local.detailQuery.description) and
local.detailQuery.description neq 0>
<cfset local.desc = local.detailQuery.description />
</cfif>
<cfreturn local.desc />
</cffunction>
Jean
On 10/16/07, Dan Wilson <[EMAIL PROTECTED]> wrote:
>
> Kevin,
>
> Congrats for teaching yourself OO. Not everyone is smart enough or
> dedicated enough to start the journey.
>
> As far as your code sample, go ahead and scope your query. Write your
> getDescription function like this:
>
> <cffunction name="getDescription">
> <cfargument name="item" required="yes" type="numeric" />
> <cfset var detailQuery = getDetail( arguments.item ) />
> <cfset var desc = "" />
> <cfif len(detailQuery.description) and detailQuery.descriptionneq 0>
> <cfset desc = detailQuery.description />
> </cfif>
> <cfreturn desc />
> </cffunction>
>
>
> A Few Points.
>
> 1) You should access the query the method. Not count on the query to be
> stored in a public variable in your cfc.
> 2) You may declare a default attribute for a cfargument, or you may
> declare required=true. Not both.
> 3) A function should not conditionally return.
>
> Hope this helps.
>
>
>
> DW
>
>
> On 10/16/07, Kevin < [EMAIL PROTECTED]> wrote:
> >
> >
> > I have been trying to teach myself cfc's and I am having trouble
> > understanding this.
> >
> > I know you should var scope any variables that are available only to
> > the instance in a call to a method.
> >
> > But should they always be scoped locally?
> > eg.
> >
> > I have a function in a products.cfc
> >
> > <cffunction name="getdetail" access="public" returntype="query">
> > <cfargument name="item" default="" required="yes" type="numeric" />
> > <cfset qry_detail = "">
> > <cfquery name="qry_detail" datasource="#variables.datasource#">
> > SELECT TITLE, DESCRIPTION FROM mytable WHERE item = <cfqueryparam
> > cfsqltype="cf_sql_integer" value="#arguments.item#" />
> > </cfquery>
> > </cffunction>
> >
> > I have another function in the same cfc that uses the query to get a
> > field if it has a value.
> >
> > <cffunction name="getdescription">
> > <cfif len(qry_detail.DESCRIPTION) and qry_detail.DESCRIPTION neq 0>
> > <cfreturn qry_detail.DESCRIPTION>
> > </cfif>
> > </cffunction>
> >
> > And i just call it in the .cfm with #products.getdescription()#.
> >
> > Now I have been reading that if the var scope is not used on the
> > query, a race condition can happen.
> >
> > But if I use
> > <cfset var qry_detail = "">
> > qry_detail.DESCRIPTION is not available to the getdescription function.
> >
> > Can a query used like this cause a race condition? Or should I redo
> > the functions to contain the query only within var scope the cfc?
> >
> > Thanks ahead.
> >
> >
> >
> > Guillaume Apollinaire quotes
> > > >
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CFCDev" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cfcdev?hl=en
-~----------~----~----~----~------~----~------~--~---