[coldbox-3.5.0] Expanding object to request context

Is there an easy way to take a simple transient domain object model (say some like a “user” object and place it’s constituent member properties (memento) into RC? I’m just trying to work out a simple way to do some of my forms processing so everything does “it” the same way and on the first past into an edit user form, I want an easy way to populate the text field “first_name” with the original value from a pre-populated domain object of “user”…since reverse engineering that by using populateModel() makes things so simple AFTER and edit and for validation, I thought there might be an easy way to do that.

Mike

i wrote a super class method that returns the memento of an object.

getPropertyMemento() will return a memento. it can even return collections and can be recursive.

`
public any function getPropertyMemento(string type=“struct”,boolean recursive=true,boolean collections=true,boolean keysUppercase=false){
var result = getEntityMememto(entity=this,recursive=arguments.recursive,collections=arguments.collections,keysUppercase=arguments.keysUppercase);

if(arguments.type == “struct”){
return result;
}else{
return getUtilities().marshallData(type=arguments.type,data=result);
}
}

private any function getEntityMememto(required any entity,any Parent,boolean recursive=true,boolean collections=true,numeric collectionDepth=0,boolean keysUppercase=false){
var objectMetaData = “”;

var entityName = “”;

var metaData = getMetaData(arguments.entity);

var propertyName = “”;

var propertyValue = “”;

var entityID = “”;

var result = structNew();

if(structKeyExists(metaData,“entityName”)){
result[“entityName”] = metaData.entityName;
}

if(structKeyExists(metaData,“properties”)){
for(var x=1;x<=arrayLen(metaData.properties);x++){
propertyName = metaData.properties[x].name;

if(arguments.keysUppercase){
propertyName = uCase(propertyName);
}

propertyValue = evaluate(“arguments.entity.get#propertyName#()”);

if(!structKeyExists(metaData.properties[x],“inject”)){
if(!isNull(propertyValue)){
if(!isObject(propertyValue)){
if(isArray(propertyValue)){
if(arguments.collections){
result[propertyName] = getArrayMemento(values=propertyValue,collectionDepth=arguments.collectionDepth,keysUppercase=arguments.keysUppercase);
}
}else{
result[propertyName] = propertyValue;
}
}else if(isObject(propertyValue) && arguments.recursive){
objectMetaData = getMetaData(propertyValue);

if(structKeyExists(objectMetaData,“entityName”)){
entityIDName = uCase(getORMService().getKey(objectMetaData.entityName));

result[propertyName & entityIDName] = evaluate(“propertyValue.get#entityIDName#()”);

if(!isNull(arguments.parent) && structKeyExists(arguments.parent,“entityName”) && arguments.parent[“entityName”] == objectMetaData.entityName){
result[propertyName] = “[parent]”;
}else{
result[propertyName] = getEntityMememto(entity=propertyValue,parent=result,collectionDepth=arguments.collectionDepth,collections=arguments.collections,keysUppercase=arguments.keysUppercase);
}
}
}
}else{
result[propertyName] = “”;
}
}
}
}

if(structKeyExists(arguments.entity,“flatten”)){
structAppend(result,arguments.entity.flatten(),true);
}

return result;
}

private any function getArrayMemento(required array values,required numeric collectionDepth,boolean keysUppercase=false){
var result = arrayNew(1);

var _collectionDepth = incrementValue(arguments.collectionDepth);

for(var x=1;x<=arrayLen(arguments.values);x++){
if(!isObject(arguments.values[x])){
arrayAppend(result,arguments.values[x]);
}else{
if(_collectionDepth >= 2){
return “[collection depth reached]”;
}else{
arrayAppend(result,getEntityMememto(entity=arguments.values[x],recursive=false,collectionDepth=_collectionDepth,keysUppercase=arguments.keysUppercase));
}
}
}

return result;
}

`

Sweet…way better than the rudimentary memento crap I wrote…thank you for this, I’ll give it a shot today.

Mike

if you include a method called “flatten” in your object, the results will also be included in the memento.

this is in case you have computed properties that you’d like returned.

an example…

`
public struct function flatten(){
var result = {};

result.fullname = getFirstName() & " " & getLastName();

return result;
}
`