Super Type Usage Methods
ColdBox Major Classes UML
The super type offers 2 methods for interacting with your model layer:
  • getInstance() - Retrieve a model object (Instead of injection)
  • populateModel() - Retrieve and/or populate a model object from the request collection.
Please also note that your models do not inherit from anything within ColdBox. They are shy and decoupled by default. If you need anything from the ColdBox environment, then you will have to inject it using our injection dsl.

getInstance()

Here is the signature
1
/**
2
* Get a instance object from WireBox
3
*
4
* @name The mapping name or CFC path or DSL to retrieve
5
* @initArguments The constructor structure of arguments to passthrough when initializing the instance
6
* @dsl The DSL string to use to retrieve an instance
7
*
8
* @return The requested instance
9
*/
10
function getInstance( name, initArguments={}, dsl )
Copied!
Examples
1
// Retrieve the User.cfc in the model folder
2
var oUser = getInstance('User');
3
// Retrieve the User.cfc in the model/users folder
4
var oUser = getInstance("users.User")
5
// Retrieve the User using an alias you mapped in your configuration binder
6
var oUser = getInstance("MyUser");
7
// Retrieve an object using a full instantation path
8
var oUtil = getInstance("mypath.utilities.MyUtil");
Copied!

populateModel()

ColdBox can populate or bind model objects from data in the request collection by matching the name of the form element to the name of a property on the object. You can also populate model objects from JSON, XML, Queries and other structures a-la-carte by talking directly to WireBox's object populator.
1
/**
2
* Populate a model object from the request Collection or a passed in memento structure
3
*
4
* @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
5
* @scope Use scope injection instead of setters population. Ex: scope=variables.instance.
6
* @trustedSetter If set to true, the setter method will be called even if it does not exist in the object
7
* @include A list of keys to include in the population
8
* @exclude A list of keys to exclude in the population
9
* @ignoreEmpty Ignore empty values on populations, great for ORM population
10
* @nullEmptyInclude A list of keys to NULL when empty
11
* @nullEmptyExclude A list of keys to NOT NULL when empty
12
* @composeRelationships Automatically attempt to compose relationships from memento
13
* @memento A structure to populate the model, if not passed it defaults to the request collection
14
* @jsonstring If you pass a json string, we will populate your model with it
15
* @xml If you pass an xml string, we will populate your model with it
16
* @qry If you pass a query, we will populate your model with it
17
* @rowNumber The row of the qry parameter to populate your model with
18
*
19
* @return The instance populated
20
*/
21
function populateModel(
22
required model,
23
scope="",
24
boolean trustedSetter=false,
25
include="",
26
exclude="",
27
boolean ignoreEmpty=false,
28
nullEmptyInclude="",
29
nullEmptyExclude="",
30
boolean composeRelationships=false,
31
struct memento=getRequestCollection(),
32
string jsonstring,
33
string xml,
34
query qry
35
)
Copied!
Examples:
1
var user = ormService.populate( ormService.new("User"), data );
2
3
// populate with includes only
4
var user = ormService.populate( ormService.new("User"), data, "fname,lname,email" );
5
6
//populate with excludes
7
var user = ormService.populate(target=ormService.new("User"),memento=data,exclude="id,setup,total" );
8
9
// populate with null values when value is empty string
10
var user = ormService.populate(target=ormService.new("User"),memento=data,nullEmptyInclude="lastName,dateOfBirth" );
11
12
// populate many-to-one relationship
13
var data = {
14
firstName = "Luis",
15
role = 1 // "role" is the name of the many-to-one relational property, and one is the key value
16
};
17
var user = ormService.populate( target=ormService.new("User"), memento=data, composeRelationships=true );
18
// the role relationship will be composed, and the value will be set to the appropriate instance of the Role model
19
20
// populate one-to-many relationship
21
var data = {
22
firstName = "Luis",
23
favColors = "1,2,3" ( or [1,2,3] ) // favColors is the name of the one-to-many relational property, and 1, 2 and 3 are key values of favColor models
24
};
25
var user = ormService.populate( target=ormService.new("User"), memento=data, composeRelationships=true );
26
// the favColors property will be set to an array of favColor entities
27
28
// only compose some relationships
29
var data = {
30
firstName = "Luis",
31
role = 1,
32
favColors = [ 1, 3, 19 ]
33
};
34
var user = ormService.populate( target=ormService.new("User"), memento=data, composeRelationships=true, exclude="favColors" );
35
// in this example, "role" will be composed, but "favColors" will be excluded
Copied!
Last modified 9mo ago
Copy link
Edit on GitHub