Products

Products
Viewing By Entry / Main
June 4, 2008

Coldbox beanfactory populatebean method

I have to say I love this automatic bean population thing going around. Model-Glue does it also but since I mostly use Coldbox, that's what I'm showing here.

WTF, you may ask? Well, you know how when you submit a form to edit a user account for example, and then you have to pass all those form elements to your user bean object's getters and setters one by one. userObj.setUsername(form.username), userObj.setUserpassword(form.password) and so on.. well you can forget about ever doing this again.

HOW!?!? By using the Coldbox beanfactory populatebean method. You can play with form elements first if you need to, then simply call the populate method passing in the current userbean, and then Coldbox will match form elements to the bean's getter and do the heaving lifting for you returning an updated bean ready for saving.

2 simple lines, get bean and populate bean. Then do whatever else you prefer from there to save it wherever etc..

<cffunction name="doEditAccount" access="public" returntype="void" output="false">
      <cfargument name="Event" type="coldbox.system.beans.requestContext">
      <!--- Logic --->
      <cfscript>
         userBean = getPlugin("sessionstorage").getVar("User");
         getPlugin("beanFactory").populateBean(userBean);
         getUsersService().saveUser(userBean);
         getPlugin("sessionstorage").setVar("User",userBean);
      </cfscript>
      <!--- Display --->
      <cfset setNextEvent("myaccount.general.dspAccountHome")>
   </cffunction>

The most important thing to remember is that your actual bean cfc object must use an init method with setters. And the form element names should be the same as the bean names.

A bean's init:

<cffunction name="init" returntype="User">
<cfargument name="username">
<cfset setUsername(arguments.username)>
</cffunction>

So that:

<input type="text" name="username">
Matches:
<cffunction name="setUsername">
<cfargument name="var">
<cfset variables.instance.username = arguments.var>
</cffunction>

Hope that helps. :)

Comments

hi........GG


I am just new to your blog and just spent about 1 hour and 30 minutes lurking and reading. I think I will frequent your blog from now on after going through some of your posts. I will definitely learn a lot from them. Regards - Gerry


This object is used to create a transfer configuration object based on the datasource information found in the ColdBox configuration file. This idea is thanks to Tom de Manincor and his musings in ColdBox-ColdSpring-Transfer. This approach let's you maintain all of your application's configuration in one single location and not create a datasource.xml for just a datasource information. It is meant to be used alongside either ColdSpring or LightWire but it can be used as a separate object too (You will have to do the wiring)....


Does this also work if I'm using Flex? I don't have the traditional HTML form fields...


Well, if your talking about using the remote coldboxproxy, yes. When you pass the arguments to the event just make sure they match the bean properties. There's really no other difference between hitting the event from an HTML form nd a Flex form by proxy.



Copyright © 2005-2006 Clint Willard. All rights reserved.
Aura skin for Clint Willard's BlogCFC inspired by Brooks Bilson's Bolg.
All trademarks property of their owners.