Memento / struct of property values

16 views
Skip to first unread message

Nando

unread,
Mar 16, 2010, 9:31:33 AM3/16/10
to cf-orm-dev
Is there a way in CF's implementation of hibernate to get a struct of property names/values from an entity? I'm going to need this several times over within my current app to return a JSON string and was wondering ...

Thanks,

Nando

Raymond Camden

unread,
Mar 16, 2010, 9:48:29 AM3/16/10
to cf-or...@googlegroups.com
Can't you just getMetaData() on the entity?

> --
> You received this message because you are subscribed to the Google Groups
> "cf-orm-dev" group.
> To post to this group, send email to cf-or...@googlegroups.com.
> To unsubscribe from this group, send email to
> cf-orm-dev+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/cf-orm-dev?hl=en.
>

--
===========================================================================
Raymond Camden, ColdFusion Jedi Master

Email : r...@camdenfamily.com
Blog : www.coldfusionjedi.com
AOL IM : cfjedimaster

Keep up to date with the community: http://www.coldfusionbloggers.org

John Whish

unread,
Mar 16, 2010, 9:52:31 AM3/16/10
to cf-or...@googlegroups.com
I don't think there is a direct way of doing it but you could do something like this:

    <cffunction name="getMemento" output="false" returntype="struct">
   <cfset var properties = ORMGetSessionFactory().getAllClassMetadata()[ ListLast( GetMetaData( this ).fullname, "." ) ].getPropertyNames()>
   <cfset var memento = {}>
   <cfset var result = "">
   <cfloop array="#properties#" index="item">
   <cfinvoke component="#this#" method="get#item#" returnvariable="result">
   <cfif IsNull(result)>
   <cfset memento[item] = "">
<cfelse>
   <cfset memento[item] = result>
    </cfif>
     </cfloop>
   <cfreturn memento>
</cffunction>

As ColdFusion doesn't understand null, I'm just setting an empty string but you could alternatively ignore the key from your struct depending on your needs. Does that help?

- John

Sam Farmer

unread,
Mar 16, 2010, 9:54:09 AM3/16/10
to cf-or...@googlegroups.com
You could just use serializeJSON(  ent ) to do this.

Cheers,

Sam

--

John Whish

unread,
Mar 16, 2010, 10:07:00 AM3/16/10
to cf-or...@googlegroups.com
You could just use serializeJSON(  ent ) to do this.

Oh, now that is nice :) 

So you could have this in an abstract entity if you want to get a struct back:

public struct function getMemento(){
  return DeserializeJSON(SerializeJSON(this));
}

much better than my suggestion! Only thing with using this is that null values are ignored, which does make sense but is worth remembering.

Nando

unread,
Mar 16, 2010, 10:25:56 AM3/16/10
to cf-or...@googlegroups.com
Thanks for the suggestions!

--
You received this message because you are subscribed to the Google Groups "cf-orm-dev" group.
To post to this group, send email to cf-or...@googlegroups.com.
To unsubscribe from this group, send email to cf-orm-dev+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cf-orm-dev?hl=en.



--
Nando M. Breiter
The CarbonZero Project
CP 234
6934 Bioggio
Switzerland

+41 91 606 6372

na...@carbonzero.ch
www.carbonzero.ch

Raymond Camden

unread,
Mar 16, 2010, 11:26:35 AM3/16/10
to cf-or...@googlegroups.com
Oh sorry - didn't see "/values" in the original question. ;)
Reply all
Reply to author
Forward
0 new messages