• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

cfscript syntax for functions(methods) in componets

New Here ,
Nov 29, 2017 Nov 29, 2017

Copy link to clipboard

Copied

I've got the following function written in tag syntax:

************

<cffunction name="myQuery" returntype="query" output="false" access="remote" hint="run some query">
   <cfargument name="someRequiredID" required="true" default="24">
  
<cfquery name="local.getThings">
  SELECT ID, entry_title
  FROM entries
  WHERE blogID =
<cfqueryparam value="#ARGUMENTS.blogid#"
  
cfsqltype="cf_sql_integer">
   
</cfquery>

  
<cfreturn local.getThings>
</cffunction>

****************

How can I rewrite this using script syntax?

ie

component  displayname="testQuery"

{

     // confusion starts here ??

     // define method myQuery

     public query function myQuery() {

          //run some query

          peopleQuery = new Query(sql="SELECT * FROM PEOPLE").execute().getResult();

          return peopleQuery;

    

     }

}

*****

Essentially, I'm not sure how to handle arguments passed to the function - and how to set the access="remote" and returnType ="query"

when using cfscript syntax.

Views

2.9K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 30, 2017 Nov 30, 2017

illiquent, you put those "other attributes" AFTER the argument definitions. So for instance, to make a method accessible to "remote" calls, use:

function testmethod (numeric x access="public" returntype="numeric") {

    return arguments.x+1;

}

[Update: after posting this, I double-checked my work and found that I was mislead in my testing to suggest the above. And I was misreading the doc below. Fortunately, what I wrote next is indeed accurate.]

You can also put them after the closing parenthesis of

...

Votes

Translate

Translate
LEGEND ,
Nov 29, 2017 Nov 29, 2017

Copy link to clipboard

Copied

I did a quick Google search, and found the following link that may be helpful:

http://www.isummation.com/blog/cfquery-using-cfscript/

HTH,

^ _ ^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 29, 2017 Nov 29, 2017

Copy link to clipboard

Copied

That link definitely helps with the query part of the function.
I'm still confused on how to replace cffunction with all its attributes with the cfscript syntax.

ie

public returnType? function runQuery() {

//where to specify attributes like name, access, returntype??

//do a query and return object

return queryObj;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 29, 2017 Nov 29, 2017

Copy link to clipboard

Copied

You can't see it, but I'm facepalming myself, right now, for having flaked on the biggest part of your request. 

Hopefully this link will help with that.

https://cfdocs.org/cffunction

V/r,

^ _ ^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 29, 2017 Nov 29, 2017

Copy link to clipboard

Copied

Something like this:

remote query function myQuery( required numeric SomeRequiredID ) {

...

}

or 

remote query function myQuery( numeric SomeRequiredID=24 ) {

...

}

You shouldn't mix required and default together in the same argument, since supplying a default value will negate it being required.

Also, if you are on CF11 or above, use queryExecute() instead of new Query().  The Query CFC provided by Adobe has numerous bugs and they are not being fixed, so treat it as a deprecated function.

-Carl

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 29, 2017 Nov 29, 2017

Copy link to clipboard

Copied

Thanks. That's what I eventually came up with as well.

Is there some place that shows how to use all the equivalent attributes for cffunction?

ie

access returntype function someFunctionName(required numeric someID)

deals with access, returntype, and required attributes from cffunction

how does one uses the other attributes (roles,resultFormat,httpMethod,produces,consumes, etc)?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 30, 2017 Nov 30, 2017

Copy link to clipboard

Copied

I think that there is an helpx.adobe.com page for it.  I'd provide the link, but our network security is so tight that I cannot access the link from work.  (Supposedly there is a fix being readied for that; but no timeline on when it will be finished or implemented.)

Google "function cfscript" and find the helpx.adobe.com page for it.

V/r,

^ _ ^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 30, 2017 Nov 30, 2017

Copy link to clipboard

Copied

Not sure of the Adobe help doc link, but CFDocs has a good CFScript page too: https://cfdocs.org/script.  There are three links at the bottom of that page that take you to additional CFScript information from community members.  Tony Junkes' pages has an example of the additional function attributes: https://github.com/tonyjunkes/cfml-tags-to-cfscript/blob/master/README.md#cffunction.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 30, 2017 Nov 30, 2017

Copy link to clipboard

Copied

Carl, those are indeed useful resources but I'm afraid they don't really address the OPs question. Neither the cfdocs.org example, nor Tony's page show using what were CFFunction attributes as methods. 😞

Actually, Tony's could have. He shows a utcOffsetToMinutes function which he converts to script, but note that he has an output="no" attribute that he does NOT represent then in the script-based function example. That's too bad, as someone like the OP might have looked there and felt it was "close but no cigar".

Also, he shows being able to declare the attributes as annotations, but I am not finding that to work. For instance, I tried putting into my example above, within the function:

/**

* @returnType numeric

*/

And then I changed the return to be:

return "ok";

The function returned "ok", even though it's not a numeric. I wonder if the annotations only really work for metadata about a function or component.

Anyway, just adding all this for the sake of completeness, for other readers of the thread. Not meant as a criticism of your attempt to help. Indeed, I know you posted your comment right about the same time I did mine, so you would not have seen me pointing to the CF docs and showing a real example that solved the OP's problem. (I have since updated it, so the date/time of my post does not seem to have been a couple hours after yours, which is too bad on this last point.)


/Charlie (troubleshooter, carehart.org)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 30, 2017 Nov 30, 2017

Copy link to clipboard

Copied

Charlie,

No worries!

Actually, Tony does show one of the additional function attributes in a sample.  In the "Utils" component, the "reverseArrayOrder " function shows the "hint" attribute being placed after the argument definitions.  That was the reason I pointed at his example in the first place.  But it doesn't show any other potential attributes, so you're comments above are still probably more useful than mine overall.

-Carl

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 30, 2017 Nov 30, 2017

Copy link to clipboard

Copied

Ah, you're right. He does. I just assumed that since he didn't with the example I saw (where he could and indeed should have), that perhaps he didn't show it anywhere. I should have looked closer. Thanks, and for the kind regards.

But really neither his page nor the Adobe docs page really draws this out as clearly as they could. I am writing up a blog post for the CF community portal (coldfusion.adobe.com) which I hope to post today, as it was a good question which was not so obvious for people to know the answer.


/Charlie (troubleshooter, carehart.org)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 30, 2017 Nov 30, 2017

Copy link to clipboard

Copied

LATEST

thanks to everyone for helping me sort that out. all suggestions helped.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 30, 2017 Nov 30, 2017

Copy link to clipboard

Copied

illiquent, you put those "other attributes" AFTER the argument definitions. So for instance, to make a method accessible to "remote" calls, use:

function testmethod (numeric x access="public" returntype="numeric") {

    return arguments.x+1;

}

[Update: after posting this, I double-checked my work and found that I was mislead in my testing to suggest the above. And I was misreading the doc below. Fortunately, what I wrote next is indeed accurate.]

You can also put them after the closing parenthesis of the methods args, as in:

function testmethod (numeric x) access="public" returntype="numeric" output="false" {

    return arguments.x+1;

}

Then again, as you already know from your example above, you can also declare the first two of those specific "attributes" implicitly at the front of the method declaration, but others do need to be after the close paren:

public numeric function testmethod (numeric x) output="false" {

    return arguments.x+1;

}

As for how one would know that they can provide what would have been cffunction attributes to a script function in the way I show above, t is indeed documented, as wolfshade indicated. The problem is that it's not in the CFML reference. Instead, see see ColdFusion Help | Defining components and functions in CFScript , which says (rather subtly and buried among lots of other info):

"The syntax to define a function is similar to the component definition:

...[snip]...

access returnType function functionName(arg1Type arg1Name="defaultValue1"

arg1Attribute="attributeValue...,arg2Type

arg2Name="defaultValue2" arg2Attribute="attributeValue...,...)

functionAttributeName="attributeValue" ... {

and the "arg1Attribute" "functionAttributeName" is an example of what they mean by what would have been an attribute to a cffunction, and note that it appears AFTER the closing parenthesis of the args.

[another edit after original posting:]

As for what they mean by the references to arg1attribute above, I honeslty don't know. the reference to 'arg1type arg1name="defaultValue1"' should be all that is needed for each arg. Anyone else know what the "arg1attribute" would be about?


/Charlie (troubleshooter, carehart.org)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 30, 2017 Nov 30, 2017

Copy link to clipboard

Copied

I'll add one more answer: I just confirmed that Pete's nifty http://cfscript.me/ tool (an online tool for converting CF tags to script) does indeed properly handle the conversion of CFFUNCTION attributes to appearing as a parenthesized list of args after the first parenthesized list of any actual args for the method. (And it shows also how the returntype and access can be declared BEFORE the method name, like I mentioned above. But if you try something like output or returnformat, you'll see those get put AFTER the method args.)


/Charlie (troubleshooter, carehart.org)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation