Model Data Binding
The framework also offers you the capability to bind incoming FORM/URL/REMOTE/XML/JSON/Structure data into your model objects by convention. This is done via WireBox's object population capabilities. The easiest approach is to use our populateModel() function which will populate the object from many incoming sources:
    request collection RC
    Structure
    json
    xml
    query
This will try to match incoming variable names to setters or properties in your domain objects and then populate them for you. It can even do ORM entities with ALL of their respective relationships. Here is a snapshot of the method:
1
/**
2
* Populate a model object from the request Collection or a passed in memento structure
3
* @model The name of the model to get and populate or the acutal model object. If you already have an instance of a model, then use the populateBean() method
4
* @scope Use scope injection instead of setters population. Ex: scope=variables.instance.
5
* @trustedSetter If set to true, the setter method will be called even if it does not exist in the object
6
* @include A list of keys to include in the population
7
* @exclude A list of keys to exclude in the population
8
* @ignoreEmpty Ignore empty values on populations, great for ORM population
9
* @nullEmptyInclude A list of keys to NULL when empty
10
* @nullEmptyExclude A list of keys to NOT NULL when empty
11
* @composeRelationships Automatically attempt to compose relationships from memento
12
* @memento A structure to populate the model, if not passed it defaults to the request collection
13
* @jsonstring If you pass a json string, we will populate your model with it
14
* @xml If you pass an xml string, we will populate your model with it
15
* @qry If you pass a query, we will populate your model with it
16
* @rowNumber The row of the qry parameter to populate your model with
17
*/
18
function populateModel(
19
required model,
20
scope="",
21
boolean trustedSetter=false,
22
include="",
23
exclude="",
24
boolean ignoreEmpty=false,
25
nullEmptyInclude="",
26
nullEmptyExclude="",
27
boolean composeRelationships=false,
28
struct memento=getRequestCollection(),
29
string jsonstring,
30
string xml,
31
query qry
32
){
Copied!
Let's do a quick example:
Person.cfc
1
component accessors="true"{
2
3
property name="name";
4
property name="email";
5
6
function init(){
7
setName('');
8
setEmail('');
9
}
10
}
Copied!
editor.cfm
1
<cfoutput>
2
<h1>Funky Person Form</h1>
3
#html.startForm(action='person.save')#
4
5
#html.textfield(label="Your Name:",name="name",wrapper="div")#
6
#html.textfield(label="Your Email:",name="email",wrapper="div")#
7
8
#html.submitButton(value="Save")#
9
10
#html.endForm()#
11
</cfoutput>
Copied!
Event Handler -> person.cfc
1
component{
2
3
function editor(event,rc,prc){
4
event.setView("person/editor");
5
}
6
7
function save(event,rc,prc){
8
9
var person = populateModel( "Person" );
10
11
writeDump( person );abort;
12
}
13
14
}
Copied!
In the dump you will see that the name and email properties have been bound.
Last modified 3yr ago
Copy link