ORM Criteria - Grouping AND and OR


I am trying to build a query with the following criteria model … ( username = X OR email = X ) AND active = true.

My code looks like this:

var criteria = UserService.newCriteria();
var User = criteria.and(
criteria.restrictions.eq( “username”, lcase( trim( arguments.username ) ) ),
criteria.restrictions.eq( “email”, lcase( trim( arguments.username ) ) )
criteria.restrictions.eq( “active”, true )

When this executes though, I am getting an error on line 182 of \coldbox\system\orm\hibernate\criterion\Restrictions.cfc that says “The add method was not found.”

Before I go tearing apart the Restrictions.cfc object, is my use of the criteria above correct?


I would do this:

var c = userService.newCriteria();
c.isTrue( “active” )
.or( c.restrictions.eq( “username”, lcase( trim( arguments.username ) ) ),
c.restrictions.eq( “email”, lcase( trim( arguments.username ) ) )

However, I have seen sometimes where ACF goes cookoo when concatenating AND and OR calls, the parser just goes cookoo. You can alternatively use: $and(), and $or() as well.


Luis F. Majano
Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Social: twitter.com/lmajano facebook.com/lmajano