Restrictions
The ColdBox restrictions class allows you to create criterions upon certain properties, associations and even SQL for your ORM entities. This is the meat and potatoes of criteria queries, where you build a set of criteria to match against or in SQL terms, your WHERE statements.
The ColdBox criteria class offers most of the criterion methods found in the native hibernate Restrictions class:
If one isn't defined in the CFML equivalent, just call it as it appears in the Javadocs and we will proxy the call to the native Hibernate class for you.

Direct Reference

You can get a direct reference to the Restrictions class via the Base/Virtual ORM services (getRestrictions()), or the Criteria object itself has a public property called restrictions which you can use rather easily. We prefer the latter approach. Now, please understand that the ColdBox Criteria Builder masks all this complexity and in very rare cases will you be going to our restrictions class directly. Most of the time you will just be happily concatenating methods on the Criteria Builder.
1
// From base ORM service
2
var restrictions = service.getRestrictions()
3
4
// From Criteria Builder
5
newCriteria().restrictions
Copied!
Ok, now that the formalities have been explained let's build some criterias.

Building Restrictions

To build our criteria queries we can use the methods in the criteria object or go directly to the restrictions object for very explicit criterions as explained above. We will also go to the restrictions object when we do conjunctions and disjunctions, which are fancy words for AND's, OR's and NOT's. To build criterias we will be calling these criterion methods and concatenate them in order to form a nice DSL language that describes what we will retrieve. Once we have added all the criteria then we can use several other concatenated methods to set executions options and then finally retrieve our results or do projections on our results.

between( property, minValue, maxValue )

Where the property value is between two distinct values
1
c.between("age",10,30);
Copied!

conjunction( required array restrictionValues )

Group expressions together in a single conjunction (A and B and C...) and return the conjunction
1
c.conjunction( [
2
c.restrictions.between("balance",100,200),
3
c.restrictions.lt("salary",20000)
4
] );
Copied!

disjunction( required array restrictionValues )

Group expressions together in a single disjunction (A or B or C...)
1
c.disjunction( [
2
c.restrictions.between("balance",100,200),
3
c.restrictions.lt("salary",20000)
4
] );
Copied!

isEQ( property, value )

Where a property equals a particular value, you can also use eq()
1
c.eq("age",30);
Copied!

isGT( property, value )

Where a property is greater than a particular value, you can also use gt()
1
c.gt("publishedDate", now() );
Copied!

gtProperty( property,otherProperty )

Where a one property must be greater than another
1
c.gtProperty("balance","overdraft");
Copied!

isGE( property,value )

Where a property is greater than or equal to a particular value, you can also use ge()
1
c.ge("age",18);
Copied!

geProperty( property, otherProperty )

Where one property must be greater than or equal to another
1
c.geProperty("balance","overdraft");
Copied!

idEQ( required any propertyValue )

Where an object's id equals the specified value
1
c.idEq( 4 );
Copied!

iLike( required string property, required string propertyValue )

A case-insensitive 'like' expression
1
c.iLike("lastName", "maj%");
Copied!

isIn( required string property, required any propertyValue )

Where a property is contained within the specified list of values, the property value can be a collection (struct) or array or list, you can also use in()
1
c.isIn( "id", [1,2,3,4] );
Copied!

isEmpty( required string property )

Where a collection property is empty
1
c.isEmpty("childPages");
Copied!

isNotEmpty( required string property )

Where a collection property is not empty
1
c.isNotEmpty("childPages");
Copied!

isFalse( required string property )

Where a collection property is false
1
c.isFalse("isPublished");
Copied!

isNull( required string property )

Where a property is null
1
c.isNull("passwordProtection");
Copied!

isNotNull( required string property )

Where a property is NOT null
1
c.isNotNull("publishedDate");
Copied!

isLT( required string property, required any propertyValue )

Where a property is less than a particular value, you can also use lt()
1
c.isLT("age", 40 );
Copied!

ltProperty( required string property, required string otherProperty )

Where a one property must be less than another
1
c.ltProperty("sum", "balance");
Copied!

isLE( required string property, required any propertyValue )

Where a property is less than or equal a particular value, you can also use le()
1
c.isLE("age", 30);
Copied!

leProperty( required string property, required string otherProperty )

Where a one property must be less than or equal to another
1
c.LeProperty("balance","balance2");
Copied!

like( required string property, required string propertyValue )

Equivalent to SQL like expression
1
c.like("content", "%search%");
Copied!

ne( required string property, required any propertyValue )

Where a property does not equal a particular value
1
c.ne("isPublished", true);
Copied!

neProperty( required string property, required any otherProperty )

Where one property does not equal another
1
c.neProperty("password","passwordHash");
Copied!

sizeEq( required string property, required any propertyValue )

Where a collection property's size equals a particular value
1
c.sizeEq("comments",30);
Copied!

sizeGT( required string property, required any propertyValue )

Where a collection property's size is greater than a particular value
1
c.sizeGT("children",5);
Copied!

sizeGE( required string property, required any propertyValue )

Where a collection property's size is greater than or equal to a particular value
1
c.sizeGE("children", 10);
Copied!

sizeLT( required string property, required any propertyValue )

Where a collection property's size is less than a particular value
1
c.sizeLT("childPages", 25 );
Copied!

sizeLE( required string property, required any propertyValue )

Where a collection property's size is less than or equal a particular value
1
c.sizeLE("childPages", 25 );
Copied!

sizeNE( required string property, required any propertyValue )

Where a collection property's size is not equal to a particular value
1
c.sizeNE("childPages",0);
Copied!

sql( required sql, params )

Use arbitrary SQL to modify the resultset
1
c.sql("char_length( lastName ) = 10");
Copied!

and( Criterion, Criterion, ... )

Return the conjuction of N expressions as arguments
1
c.and( c.restrictions.eq("name","luis"), c.restrictions.gt("age",30) );
Copied!

or( Criterion, Criterion, …. )

Return the disjunction of N expressions as arguments
1
c.or( c.restrictions.eq("name","luis"), c.restrictions.eq("name", "joe") );
Copied!

isNot( required any criterion )

Return the negation of an expression. You can also use not()
1
c.isNot( c.restrictions.eg("age", 30) );
Copied!

isTrue( required string property )

Returns if the property is true
1
c.isTrue("isPublished");
Copied!
Adobe ColdFusion may throw an "Invalid CFML construct" error for certain CBORM methods that match reserved operator names, such as .and(), .or(), and .eq(). You can use .$and(), .$or(), and .isEq() to avoid these errors and build cross-engine compatible code.
In some cases (isEq(), isIn(), etc), you may receive data type mismatch errors. These can be resolved by using JavaCast on your criteria value or use our auto caster methods: idCast(), autoCast()
1
c.isEq("userID", idCast( 3 ) );
Copied!
You can also use the add() method to add a manual restriction or array of restrictions to the criteria you are building.
1
c.add( c.restrictions.eq("name","luis") )
Copied!
But as you can see from the code, the facade methods are much nicer.

Negation

Every restriction method you see above or in the docs can also be negated very easily by just prefixing the method with a not .
1
c
2
.notEq("userID", idCast( 3 ) )
3
.notBetween()
4
.notIsTrue()
5
.notIsFalse();
Copied!

when()

There are times where you need if statements in order to add criterias based on incoming input. That's ok, but we can do better by adding a when( test, target ) function that will evaluate the test argument or expression. If it evaluates to true then the target closure is called for you with the criteria object so you can do your criterias:
1
newCriteria()
2
.when( isBoolean( arguments.isPublished ), function( c ){
3
// Published process
4
c
5
.isEq( "isPublished", isPublished )
6
.when( isPublished, function( c ){
7
c.isLt( "publishedDate", now() )
8
.$or(
9
c.restrictions.isNull( "expireDate" ),
10
c.restrictions.isGT( "expireDate", now() )
11
)
12
.isEq( "passwordProtection","" );
13
})
14
}
15
} )
16
.when( !isNull( arguments.showInSearch ), function( c ){
17
c.isEq( "showInSearch", showInSearch );
18
} )
19
.when( arguments.isActive, function( c ){
20
c.isTrue( "isActive" );
21
})
22
.list()
Copied!
Export as PDF
Copy link
Edit on GitHub
Contents
Direct Reference
Building Restrictions
between( property, minValue, maxValue )
conjunction( required array restrictionValues )
disjunction( required array restrictionValues )
isEQ( property, value )
isGT( property, value )
gtProperty( property,otherProperty )
isGE( property,value )
geProperty( property, otherProperty )
idEQ( required any propertyValue )
iLike( required string property, required string propertyValue )
isIn( required string property, required any propertyValue )
isEmpty( required string property )
isNotEmpty( required string property )
isFalse( required string property )
isNull( required string property )
isNotNull( required string property )
isLT( required string property, required any propertyValue )
ltProperty( required string property, required string otherProperty )
isLE( required string property, required any propertyValue )
leProperty( required string property, required string otherProperty )
like( required string property, required string propertyValue )
ne( required string property, required any propertyValue )
neProperty( required string property, required any otherProperty )
sizeEq( required string property, required any propertyValue )
sizeGT( required string property, required any propertyValue )
sizeGE( required string property, required any propertyValue )
sizeLT( required string property, required any propertyValue )
sizeLE( required string property, required any propertyValue )
sizeNE( required string property, required any propertyValue )
sql( required sql, params )
and( Criterion, Criterion, ... )
or( Criterion, Criterion, …. )
isNot( required any criterion )
isTrue( required string property )
Negation
when()