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…
-
property type="full.path.name.serviceBeanName" name="serviceBeanName" getter="true" setter="true";
Thuscan then be called as simply serviceBeanName…[my function name]
- Query’s look a little different:
myQry = new Query(); myQry.setDatasource("myDSName"); myQry.setSQL("select top 10 * from myTable"); writedump(myQry.execute().getResult());
NOTE: can’t get a recordcount using getResult() from anything but a select statement. Instead use
myQry.execute().getPrefix().recordCount
- 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.
myQry.setSQL("select * from myTablewhere myID in (:idList)"); offeringsQry.addParam(name="idList", value="1,2,3", cfsqltype="cf_sql_integer", list="true");
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
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"; }
DAO and GATWAY
Now for your DAO or Gateway. **note** these two need INIT functions to set dsn
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...