Unable to populate a new entity

40 views
Skip to first unread message

Joe Brislin

unread,
Jun 3, 2010, 11:30:24 AM6/3/10
to framew...@googlegroups.com
Is there any reason that I shouldn't be able to use the variables.fw.populate() function to populate a new orm entity? The save function in my controller creates a new entity if the passed id equals to 0 so that I can create a new record. When I try to populate it, the values remain as empty strings. Any one have any ideas if I'm doing something wrong here?

            transaction{
                if(rc.contactID NEQ 0){
                    rc.contact = EntityLoadbyPK("contacts",rc.contactID);
                }else{
                    rc.contact = EntityNew("contacts");
                }
                variables.fw.populate(rc.contact);
               
                EntitySave(rc.contact);
                rc.id = rc.contact.getContactID();
            }

I added a dump after the populate method and all strings were empty still.

Thanks,
Joe Brislin

Shane Pitts

unread,
Jun 3, 2010, 11:43:43 AM6/3/10
to framew...@googlegroups.com

public void function populate( any cfc, string keys = "", boolean trustKeys = false )

Automatically populates an object with data from the request context. For any public method setKey() on the object, if rc.key exists, call the method with that value. If the optional list of keys is provided, only attempt to call setters for the specified keys in the request context. Mainly useful for populating beans from form posts. Whitespace is permitted in the list of keys for clarity, e.g., “firstname, lastname, email”. This approach relies on explicitly defined setter methods in the object. It won’t detect and use onMissingMethod() or ColdFusion 9’s property-based setter methods.

For populate() to work with onMissingMethod() or ColdFusion 9’s property-based setters, you need to specify trustKeys = true. If you specify the list of keys, populate() will not test whether the setter exists, it will just call it – which means that onMissingMethod() and ColdFusion 9’s property-based setters will be invoked automatically. If you omit the keys, be careful because populate() will cycle through the entire request context and attempt to set properties on the object for everything! A try/catch is used to suppress any exceptions in this case but you need to be aware that this may be a little dangerous if you have a lot of data in your request context that does not match the properties of the object! If an exception is caught, onPopulateError() is called with the object, property name and the request context structure as its three arguments. The default behavior is to simply ignore the exception but you can override that if you want – see onPopulateError() above.


--
FW/1 on RIAForge: http://fw1.riaforge.org/
 
FW/1 on github: http://github.com/seancorfield/fw1
 
FW/1 on Google Groups: http://groups.google.com/group/framework-one

Shane Pitts

unread,
Jun 3, 2010, 11:47:19 AM6/3/10
to framew...@googlegroups.com
Sorry, <tab enter> = submit :/

I was just wondering if you're using CF9, and if the trusted keys issue had anything to do with it?  I always set a key list when I use the populate, just so it doesn't loop through everything in RC. but I don't think it's required..  

Joe Brislin

unread,
Jun 3, 2010, 11:57:33 AM6/3/10
to framework-one
Shane,

Thanks for the help. It would have helped if I slowed down and paid
attention to what I read in the reference manual. I had seen the same
snippet that you posted here. I am using CF9. I set trustedKeys=true
and lo and behold everything populated.

- Joe
Reply all
Reply to author
Forward
0 new messages