Professional Web Applications Themes

Query Objects in cfloop and cfoutput - Coldfusion - Advanced Techniques

Ive found something very annoying. It seems the query attribute of cfloop and cfoutput cant accept a query method from a cfc object. For example I have a object with a method called queryresult. I would expect this to work: <cfloop query="variables.ThisObject.QueryResult()"> #TableColumn1# </cfloop> but it does not. rather I have to set the query into another variable and then use it like this: <cfset variables.ThisResult = variables.ThisObject.QueryResult() /> <cfloop query="variables.ThisResult"> #TableColumn1# </cfloop> so why does this matter? well is just is a pain thats all it also made a recursive function I was writeing a lot more complex and ...

  1. #1

    Default Query Objects in cfloop and cfoutput

    Ive found something very annoying. It seems the query attribute of cfloop and
    cfoutput cant accept a query method from a cfc object. For example I have a
    object with a method called queryresult. I would expect this to work:

    <cfloop query="variables.ThisObject.QueryResult()">
    #TableColumn1#
    </cfloop>

    but it does not. rather I have to set the query into another variable and then
    use it like this:
    <cfset variables.ThisResult = variables.ThisObject.QueryResult() />
    <cfloop query="variables.ThisResult">
    #TableColumn1#
    </cfloop>

    so why does this matter? well is just is a pain thats all it also made a
    recursive function I was writeing a lot more complex and I had to end up
    manageing the query results in a custom structure.

    This should probably be fixed in the next version.


    Ryan Guest

  2. #2

    Default Re: Query Objects in cfloop and cfoutput

    What does the queryresult() method return?


    Dan Guest

  3. #3

    Default Re: Query Objects in cfloop and cfoutput

    CFLOOP accepts a query name, not a query object. Are you using it correctly?
    mxstu Guest

  4. #4

    Default Re: Query Objects in cfloop and cfoutput

    queryresult() returns a query result,

    I dont have a question, Im just saying that it should work that way. and BTW
    obviously if I set the result into another variable then it does not accept
    just the name of a query because there was never a query named
    variables.ThisResult...thats a made up local variable. So at some level the
    query attribute is accepting any variable that contains a query result. with
    the obvious exception of an object method.


    Ryan Guest

  5. #5

    Default Re: Query Objects in cfloop and cfoutput

    To put it more explictly, cfloop is looking for a simple value, whether it be a
    query name or a variable name. The reason it works in your second example is
    because you are passing in a simple value, the literal string
    "variables.ThisResult".

    <cfloop query="variables.ThisResult">
    #TableColumn1#
    </cfloop>

    If you were using this....

    <cfloop query="#variables.ThisResult#">
    #TableColumn1#
    </cfloop>

    ... variables.ThisResult would be evaluated and cfloop would throw an error
    because you are trying to pass in a complex object where it expects a simple
    value. This is the same problem with the original example:

    <cfloop query="variables.ThisObject.QueryResult()">

    Maybe you will get your wish and they will add "queryObect" as an attribute in
    some other release, but as it stands, that is not the way it works now.

    mxstu Guest

  6. #6

    Default Re: Query Objects in cfloop and cfoutput

    Actually thats not quite right. If it were the complex/simple issue then you
    would get an error of "Complex object types cannot be converted to simple
    values."

    as it stands if you try this you get an error like
    'The value of the attribute query, which is currently
    "variables.ThisObject.QueryResult()", is invalid.' Which implies to me that the
    query attribute cannot trigger the object method...or something??

    it seems like its a bad limitation there are other places where you can run
    into this in non-object ways. for example. Lets say you populate a struct or
    array with several query results:

    <cfquery name="myquery" datasource="x" sql="SELECT * FROM tblUser">
    <cfset Struct = structnew() />
    <cfset Struct.One = myquery />
    <cfset Struct.two = myquery />

    a direct call to the structure works:
    <cfloop query="Struct.One">

    but if you try to get dymanic by useing a key you can only do it by building
    the string out like this:
    <cfset WhichOne = 'one' />
    <cfloop query="Struct.#WhichOne#">

    It wont work if you try to use structfind to return the value with the result:
    <cfset WhichOne = 'one' />
    <cfloop query="StructFind(Struct,WhichOne)">

    BTW, the IsQuery function will work with any of these and return true as long
    as the value contains by the return is a query. My hope...and I hope
    macromedia is listeing here...is that the query attribute operate in the same
    way and accept a result regardless of how it is refrenced. a pointer to a query
    result should be the same regardless.


    Ryan Guest

  7. #7

    Default Re: Query Objects in cfloop and cfoutput

    Argh! I pasted that last post and left out the most important part. A simple
    value "...that represents a query object." My bad.

    Yeah, I hear what you're saying, but ... the string
    "variables.ThisObject.QueryResult()" doesn't technically represent a query
    object. It represents a method that happens to return a query object. So I
    would assume <cfloop query="Struct.One"> works because "One" does represent an
    actual query object (myquery). Btw, this is guessing on my part based on the
    doentation and observed behavior.

    I would expect IsQuery() to work, assuming you're calling it this way:

    <cfif IsQuery(variables.ThisObject.QueryResult())>
    .... true ...
    </cfif>

    Because variables.ThisObject.QueryResult() is evaluated, thus returning a
    query which is passed to the IsQuery() function.












    mxstu Guest

  8. #8

    Default Re: Query Objects in cfloop and cfoutput

    Btw, the reason for the last comment about IsQuery() is that the IsQuery
    function will accept a query object, not just a query name: For example this
    returns yes

    <cfoutput>#IsQuery(myQuery)#</cfoutput><br>

    ... but this returns no....

    <cfoutput>#IsQuery("myQuery")#</cfoutput>



    mxstu Guest

Similar Threads

  1. CFOUTPUT QUERY rowcount = 0
    By jack456 in forum Coldfusion - Getting Started
    Replies: 3
    Last Post: September 23rd, 02:04 PM
  2. cfoutput compared to cfloop
    By kicker79 in forum Macromedia ColdFusion
    Replies: 2
    Last Post: July 21st, 11:23 AM
  3. Listbox, CFOUTPUT, and CFLOOP
    By sage703 in forum Coldfusion Database Access
    Replies: 3
    Last Post: June 16th, 05:35 PM
  4. concat cfloop index to cfoutput variable inside loop?
    By Damnit Spock! in forum Macromedia ColdFusion
    Replies: 0
    Last Post: April 8th, 09:03 PM
  5. cfoutput and cfloop
    By raulriera in forum Coldfusion Database Access
    Replies: 4
    Last Post: March 30th, 06:39 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139