CFScript Components and OO notes
Let’s start with a few notes and gotcha’s:
- the attribute: accessors=”true” in combination with the property names, takes away any need for setters or getters.
- Catch is done outside of the Try block. so try {} catch(any e) {}
- Pete Freitag’s Cheat Sheet for loops and such is very helpful.
- thanks the accessors attr) allows you to use the just the name of the bean. So…
-
[code language=”javascript”]property type="full.path.name.serviceBeanName" name="serviceBeanName" getter="true" setter="true";[/code]
Thuscan then be called as simply serviceBeanName…[my function name]
- Query’s look a little different:
[code language=”javascript”]
myQry = new Query();
myQry.setDatasource("myDSName");
myQry.setSQL("select top 10 * from myTable");
writedump(myQry.execute().getResult());[/code]NOTE: can’t get a recordcount using getResult() from anything but a select statement. Instead use
[code language=”javascript”]myQry.execute().getPrefix().recordCount[/code]
- Gotcha Alert! There’s a little trick to getting a where-in clause to work. See here for original post, but also spread the word.
[code language=”javascript”]myQry.setSQL("select * from myTablewhere myID in (:idList)");
offeringsQry.addParam(name="idList", value="1,2,3",
cfsqltype="cf_sql_integer", list="true");[/code]
The key is to use the LIST parameter.
BEAN
This is your new bean. say hello! no getters. no setters. That’s taken care of by using properties, and the @setters “true” syntax at the top. Note that all that syntax at the top is VERY specific. spaces, not tabs. no commas or semicolons.
- not even the name of the bean. that’s implicit in the name of the file.
- component attributes don’t use commas and come before curly braces
[code language=”javascript”]
component
output="false"
hint="I am the bean of the blah objects."
accessors="true"
{
property type="numeric" name="blahID" default="" getter="true" setter="true";
property type="string" name="foo" default="" getter="true" setter="true";
} [/code]
DAO and GATWAY
Now for your DAO or Gateway. **note** these two need INIT functions to set dsn
[code language=”javascript”]
component
output="false"
hint="sets objects in DB."
accessors="true"
extends = "tools.mybasetools"
{
property type="string" name="DSN" getter="true" setter="true";</pre>
public full.path.name.of.datatype.bean function init(required string DSN){
variables.dsn = arguments.dsn;
return this;
}
// public and private functions below… [/code]