Model Data Binding
Easily bind incoming data into your models.
ColdBox allows you to populate data from FORM/URL/REMOTE/XML/JSON/Struct
into your model objects by convention. This is done via WireBox's object population capabilities.
populate()
The super type has a method called populate
which you will use to trigger the data binding. The supported incoming data sources are:
request collection RC
structs
json
xml
query
The method will try to match the incoming variables names in the data packet to a setter
or property
in the target model object. If there is a match, then it will inject that data into the model or call it's setter
for you.
Let's explore the API
Examples
Let's do a quick example. Here is a Person.cfc
that has two properties with automatic getters/setters.
Person.cfc
editor.cfm
Here is an editor to submit a form to create the person.
Event Handler -> person.cfc
Here is an event handler to do the saving
Run the code, and you will see that the populator matched the incoming variables into the model and thus binding it.
ORM Integration
The populator can also bind ORM objects natively. However, it can also build out relationships via the composeRelationships
argument. This allows you to create the objects from simple identifiers for the following relationships:
one-to-one
: The id of the relationshipmany-to-one
: The id of the parent relationshipone-to-many
: A list of identifiers to link to the target object which ends up being an array of objectsmany-to-many
: A list of identifiers to link to the target object which ends up being an array of objects
So if we have a User
object with a many-to-one
of Role
and a many-to-many
of Permissions
, we could populate the User
with all of it's data graph:
This packet has the two relationships role
and permissions
as an incoming list. The list can be a simple string or an actual JSON array. To populate we can do this:
That's it. ColdBox will take care of matching and building out the relationships.
You can also use the nullEmptyInclude
and nullEmptyExclude
properties to include and exclude null
value population.
You can also use ignoreEmpty
to ignore all the empty values on population so the ORM treats them as null
Mass Population Control: this.population
this.population
ColdBox also supports the ability for target objects to dictate how they will be populated. This convention allows for objects to encapsulate the way the mass population of data is treated. This way, you don’t have to scrub or pass include excludes lists via population arguments; it can all be nicely encapsulated in the targeted objects. Just declare a this.population
in your object's pseudo constructor:
The populator will look for a this.population
struct with the following keys:
include
: an array of property names to allow population ONLYexclude
: an array of property names to NEVER allow population
Ignoring Mass Population
The population methods also have an argument called: ignoreTargetLists
which defaults to false
, meaning it inspects the objects for these population markers. If you pass it as true
then the markers will be ignored. This is great for the population of objects from queries or an array of structs or mementos that YOU have control of. Great in DAOs or service layers.`
Last updated