I'm currently building my own MVC in ColdFusion 9 (cfscript) and for some reason I am having a little trouble in the Base Model that I have created.
public function init(required itemName){
var columns = {};
var result = '';
var type = '';
var length = '';
var key = '';
var field = '';
var allowNull = '';
setModel(itemName);
this.table = this.model.tableName();
this.qdb = request.qwerkfactory.newQDB();
this.qdb.init(tableName = this.table);
this.qdb.getTableAttributes();
this.result = this.qdb.execute().getResult();
for(i = 1; i LT this.result.recordcount; i++){
result = this.result;
type = ReReplaceNoCase(result['type'][i], '[0-9()]', '', 'all');
length = ReReplaceNoCase(result['type'][i], '[a-z()]', '', 'all');
key = (len(result['key'][i])) ? result['key'][i] : false;
field = result['field'][i];
allowNull = result['null'][i];
columns["#field#"] = {};
columns["#field#"].type = "#type#";
columns["#field#"].length = "#length#";
columns["#field#"].key = "#key#";
columns["#field#"].null = "#allowNull#";
}
setTableAttributes(columns);
return this.model;
}
This is my method that acts as a constructor method for my Model component. What it's doing is geting the table schema for a particular table and then it will return it to a model that inherits this base class. However, I have defined a property for the component called tableColumns, when I set it in a setter method:
public function setTableAttributes(required struct attr){
this.tableColumns = arguments.attr;
}
It sets it fine at this point, when I writeDump() this.tableColumns it returns the correct result. But when using the getter it says tableColumns is undefined in this. What on earth am I doing wrong?
The problem is form your use of the this
scope. Properties of CFCs are stored in the variables
scope of the CFC..which is different than this
.
Simply change your code above to use the variables
scope instead of this
scope and your getters will work as expected.