Skip to content

Instantly share code, notes, and snippets.

@dustinmartin
Created April 29, 2011 00:36
Show Gist options
  • Save dustinmartin/947636 to your computer and use it in GitHub Desktop.
Save dustinmartin/947636 to your computer and use it in GitHub Desktop.
Populate Method for CF9 ORM
<cfscript>
public void function populate(required struct memento,
boolean trustedSetter=false,
string include="",
string exclude="",
string disallowConversionToNull=""){
var object = this;
var key = "";
var populate = true;
// Populate Bean
for(key in memento){
populate = true;
// Include List?
if( len(include) AND NOT listFindNoCase(include,key) ){
populate = false;
}
// Exclude List?
if( len(exclude) AND listFindNoCase(exclude,key) ){
populate = false;
}
// Populate?
if( populate ){
// Check if the setter exists or if trusted setting is on
if( structKeyExists(object,"set" & key) or trustedSetter ){
// If the value is blank it might be converted to null
if( isSimpleValue(memento[key]) && trim(memento[key]) == "" ){
// Check if this value should be kept as a string instead of converted to null
if( len(disallowConversionToNull) AND NOT listFindNoCase(disallowConversionToNull,key) ){
evaluate("object.set#key#(memento[key])");
}
else {
// Set the value to null
evaluate('object.set#key#(javacast("null",""))');
}
}
else {
evaluate("object.set#key#(memento[key])");
}
}
}
}
}
</cfscript>
<cfscript>
if(form.value== ""){
object.setValue1( javacast("null","") );
}
else {
object.setValue1(form.value);
}
</cfscript>
<cfscript>
object.populate(form);
</cfscript>
<cfscript>
object.populate(
memento = form,
disallowConversionToNull = "someProperty,anotherProperty"
);
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment