|
Attached is the BeanUtil
CFC. The core functionality I borrowed from ColdSpring. My first version didnt
take into consideration objects which extended others, hence it wasnt smart
enough to only call the top level methods and it ended up calling super-class
methods of the same name (bad!). Anyways, I usually use it like
this:
<cfset Person =
CreateObject('component', 'Person')>
<cfset BeanUtil =
CreateObject('component', 'BeanUtil')>
<cfset Person =
BeanUtil.populateBean(Person, FORM)>
Assume that Person has
methods "setFirstName()", "setLastName()", "setAge()", etc. and FORM looks
like:
FORM.firstname =
"john"
FORM.lastname =
"doe"
FORM.age =
"156"
Then after the
populateBean() call above, the Person object has been set
appropriately.
If you are using a
framework like MachII or Model-Glue you can write a filter that will wire up all
of this together before an event takes place. It can all be pretty automatic.
Since you dont want to always be creating the BeanUtil object, I usually place
it in App scope at startup and use it from there. All variables should be var'ed
so its thread-safe.
/Cody From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Ron Phillips Sent: Thursday, March 30, 2006 3:58 AM To: [email protected] Subject: RE: [CFCDev] passing from values to bean I'd be interested in some code, please.
Ron
>>> [EMAIL PROTECTED] 3/29/2006 3:49 PM >>> You can write a Util function/class to populate a bean from a
struct of data. ColdSpring has this functionality and I wrote a simplified
version that takes a bean and a struct as input. The object loops over the
struct keys and if an appropriate "set" method exists in the bean (using
introspection you can retrieve the methods of a bean/CFC) and then calls the set
method with the struct key-value as input. Usually I just pass in the whole FORM
scope to my Util object and all form fields are then auto-populated into my
bean.
Let me know if you're interested and I might be able to pass along
some code.
/cody From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Joe Ferraro Sent: Wednesday, March 29, 2006 12:25 PM To: [email protected] Subject: Re: [CFCDev] passing from values to bean I remember back in the day talking about the arguments scope and noting that though it looks like a struct it is actually a scope and doesn't exactly work the same way. I don't know if that has changed at in the new release of CF. Since form is a scope too I wonder if you would encounter any struct related incompatability issues there. The issues were minor. I think something like the key count returning different numbers based on if you were using ver 6 or 7 On 3/29/06, Chris
Stoner <[EMAIL PROTECTED]>
wrote:
---------------------------------------------------------- You are subscribed to cfcdev. To unsubscribe, send an email to [email protected] with the words 'unsubscribe cfcdev' as the subject of the email. CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting (www.cfxhosting.com). An archive of the CFCDev list is available at www.mail-archive.com/[email protected] ______________________________________________________________________ This email has been scanned by the MessageLabs Email Security System. For more information please visit http://www.messagelabs.com/email ______________________________________________________________________ ---------------------------------------------------------- You are subscribed to cfcdev. To unsubscribe, send an email to [email protected] with the words 'unsubscribe cfcdev' as the subject of the email. CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting (www.cfxhosting.com). An archive of the CFCDev list is available at www.mail-archive.com/[email protected] You are subscribed to cfcdev. To unsubscribe, send an email to [email protected] with the words 'unsubscribe cfcdev' as the subject of the email. CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting (www.cfxhosting.com). An archive of the CFCDev list is available at www.mail-archive.com/[email protected] ---------------------------------------------------------- You are subscribed to cfcdev. To unsubscribe, send an email to [email protected] with the words 'unsubscribe cfcdev' as the subject of the email. CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting (www.cfxhosting.com). An archive of the CFCDev list is available at www.mail-archive.com/[email protected] |
BeanUtil.cfc
Description: Binary data
