How to include cfquery-tag and/or cfml in models written in cfscript?

147 views
Skip to first unread message

Thorsten Eilers

unread,
Jan 19, 2013, 3:53:59 AM1/19/13
to cfwh...@googlegroups.com
Good morning,

generally I have all my models written in cfscript like this.

However now I would like to use some functions, or only cfquery in the middle of the cfscript code, because I prefer the cfscript tag because of it better readibility.

How can I make this function written in cfml to work in this component written in cfscript?
I already tried a zillion times to enter <cfscript> tags, but obviosly at the wrong places.

Regards
Thorsten


component
    extends="Model"
    hint="Base Useractivity model"
{
    public void function init() {   
        table("users_useractivities");                                                                                        
        hasMany(name="AssociationUsersToUseractivities", dependent="deleteAll");
                                       
    }
           
   
    <cffunction name="getUseractivities" returntype="query">
        <cfset var qryUseractivities = "">

        <cfquery name="qryUseractivities">
                SELECT  *
                   
            FROM    mytable_AssociationUsersToUseractivities
        </cfquery>

        <cfreturn qryUseractivities>
    </cffunction>

}

Tom King

unread,
Jan 19, 2013, 4:29:22 AM1/19/13
to cfwh...@googlegroups.com
I think you'd need to have your component definition in tags; then your cfscript blocks can have whatever in them.
Won't work the other way round, as you're trying to nest <cftags> within the overall script block.

Thorsten Eilers

unread,
Jan 19, 2013, 4:58:17 AM1/19/13
to cfwh...@googlegroups.com
Hi Tom. I guess you are right. I hoped to prevent this.
It's a pity that there is no easy readably syntax for dummies like me for a query tag in cfscript. ;-)
<cfquery> is still King, Mr. King!. ;-)

Simon Allard

unread,
Jan 19, 2013, 9:54:37 AM1/19/13
to cfwh...@googlegroups.com
Hi Thorsten,

This is how you can write query using cfscript.

var loc = {};
loc.query1 = new query();
loc.query1.addParam(name="someName", value="somevalue", cfsqltype="cf_sql_varchar"); 
loc.query1.setAttributes(
dataSource="datasourname",
SQL="SELECT * FROM tableName WHERE something = :someName"
);

loc.query1 = loc.query1.execute();
loc.queryResults1 = loc.query1.getResult();

Query of Query are written like this:

loc.query2 = new query();
loc.query2.setAttributes(
dbtype="query",
QoQ=loc.query1,
SQL="SELECT * FROM QoQ ORDER BY something ASC"
);

loc.query2 = loc.query.execute();
loc.queryResults2 = loc.query2.getResult();


Even though i've switched to <cfscript> syntax, I still prefer the old <cfquery> way. Hope it helps!

A+

Simon




--
You received this message because you are subscribed to the Google Groups "ColdFusion on Wheels" group.
To view this discussion on the web visit https://groups.google.com/d/msg/cfwheels/-/7DNykNPLigcJ.

To post to this group, send email to cfwh...@googlegroups.com.
To unsubscribe from this group, send email to cfwheels+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cfwheels?hl=en.

tpet...@gmail.com

unread,
Jan 21, 2013, 7:49:45 PM1/21/13
to cfwh...@googlegroups.com
does anyone else think that the query syntax in cfscript is hideous, or is it just me?

Simon Allard

unread,
Jan 21, 2013, 10:33:30 PM1/21/13
to cfwh...@googlegroups.com

Agreed!

Took me a while to figure out how to do a proper QoQ in cfsript...lol

The thing I like with wheels is that I can write less and do more!

A+

Simon

To view this discussion on the web visit https://groups.google.com/d/msg/cfwheels/-/OVmyBoXA8UkJ.

Chris Geirman

unread,
Jan 22, 2013, 1:26:12 AM1/22/13
to cfwh...@googlegroups.com

Beyond hideous!

Chris Peters

unread,
Jan 22, 2013, 9:17:08 AM1/22/13
to cfwh...@googlegroups.com
Definitely agreed. Unnecessarily complex, which is pretty contrary to the principles that CFML was founded on. It feels like writing plain Java!


To view this discussion on the web visit https://groups.google.com/d/msg/cfwheels/-/OVmyBoXA8UkJ.

Andy Bellenie

unread,
Jan 22, 2013, 11:55:57 AM1/22/13
to cfwh...@googlegroups.com
Agreed

Simeon C

unread,
Jan 23, 2013, 3:12:24 PM1/23/13
to cfwh...@googlegroups.com
I know that in Railo we can do the following in cfscript:

query name="qryUseractivities"{
       echo("SELECT  *");
       echo("FROM    mytable_AssociationUsersToUseractivities ");
}

When you get your head around the shell script echo functions it works the same as cfquery just in script form, you can even use queryparam inside the braces as well.

James Harvey

unread,
Jan 24, 2013, 8:58:25 PM1/24/13
to cfwh...@googlegroups.com
There is also a 'single line' way of writting a query in cfscript:

Queryname = new Query(name="queryname", datasource="get('datasourcename')", cachedwithin="createtimespan(0,0,5,0)", sql="select * from yourtable where foo='#params.bar#'").execute().getResult();

This treats it like an object, so you could actually do a call in a view or something for:

#queryname.recordcount#

You could also loop over your query right after calling it:

For(x=1; x <= queryname.recordcount; x=x+1){
...some logic here...
//say we want to right some output:
Writeoutput('this is '&x);
}

-James

Reply all
Reply to author
Forward
0 new messages