If I have an object instance with some properties,...
# lucee
c
If I have an object instance with some properties, it there a way to get all the properties as a struct without having to call every getter?
z
this
?
c
I’ve created an instance using ‘new foo()’ and now want to do something like ‘foo.properties()’. Is this possible?
j
getMetaData(object)
might work. It should return a bunch of stuff including the object's properties.
Oh you probably want the values? Never mind. I need my caffeine.
s
☝🏻 1
👍 2
i
If you are using ORM and its persistent, you can always return a struct by doing doing an HQL query and using [select new map(column1, column2, etc) from Table where xxx]. It returns an array of structs. If you don't pass any column it may just do the entire component. don't remember.
c
Thanks all.. FWIW, I ended up writing a small utility function for this purpose..
Copy code
// Returns a struct of the object's property key/values
// NOTE: for local.meta.properties to exist, the properties must be declared with cfproperty
public static struct function getProperties(required object object) {
	local.properties = {};
	for (local.property in GetMetadata(arguments.object).properties;) {
		local.properties[local.property.name] = arguments.object[local.property.name];
	}
	return local.properties;
}
i
@chapmandu Does that work? Calling arguments.object[local.property.name] instead of a getter?
s
Yes. You can also call methods that way:
object[ methodName ]()
🙌 1
i
@sknowlton Good to know. Thanks. Yeah, looks like I had to enable Implicit notation to get it working. Java has property access like this, so I always thought it made sense but just always using the accessors. You live and learn.