Model Data Binding
Easily bind incoming data into your models.
Last updated
Was this helpful?
Easily bind incoming data into your models.
Last updated
Was this helpful?
ColdBox allows you to populate data from FORM/URL/REMOTE/XML/JSON/Struct
into your model objects by convention. This is done via capabilities.
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
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.
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 relationship
many-to-one
: The id of the parent relationship
one-to-many
: A list of identifiers to link to the target object which ends up being an array of objects
many-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.
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 ONLY
exclude
: an array of property names to NEVER allow 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.`