[coldbox 3.5.3] criteria builder associations

There is an association named ‘user’ in my entity.

this works:

users= criteria.createAlias(“user”,“u”).ilike(“u.firstname”,"%#firstname#%").list();

I am trying to add an OR clause that will also check “lastname”, too.

trying something like this, ORM and cfbuilder doesnt like it:

users= criteria.createAlias(“user”,“u”).ilike(“u.firstname”,"%#firstname#%").or(criteria.ilike(“u.lastname”,"%#lastname#%")).list();

How can I add an OR clause that will also do a like on another field?

Try or( restriction1, restriction2 )
http://wiki.coldbox.org/wiki/Extras:CriteriaBuilder.cfm

Tried this, but didnt work:

users = criteria.createAlias(“user”,“u”).ILIKE(“u.firstname”,"%#firstname#%").OR(criteria.restrictions.ILIKE(“u.lastname”,"%#lastname#%")).list();

Put both restrictions in the or()

tried this but didnt work:

users = criteria.createAlias(“user”,“u”).OR(criteria.restrictions.ILIKE(“u.firstname”,"%#firstname#%"),criteria.restrictions.ILIKE(“u.lastname”,"%#lastname#%")).list();

this was the error message:

An unhandled exception has occurred. Please look at the diagnostic information below:
Type Template
Message Invalid CFML construct found on line 36 at column 62.
Detail ColdFusion was looking at the following text:

OR

The CFML compiler was processing:

  • An expression beginning with criteria.createAlias, on line 36, column 29.This message is usually caused by a problem in the expressions structure.
  • A script statement beginning with caseUsers on line 36, column 17.
  • A script statement beginning with public on line 17, column 9.

it doesnt like the OR part.

Put both restrictions in the or()

With that last snippet, didnt I do that?

Move each restriction into the or and comma separate.

users = criteria.createAlias(“user”,“u”).OR(criteria.restrictions.ILIKE(“u.lastname”,"%#lastname#%"),criteria.restrictions.ILIKE(“u.firstname”,"%#firstname#%")).list();

Curt

Ah, sorry, didn’t see that.

Yeah, I’m seeing the same thing as you. The OR() won’t work when chained.

So you can either do:

c.createAlias(…);
c.or( …).list();

Or you can use a disjunction:

c.createAlias( …).disjunction( [
c.restrictions.iLike(…),
c.restrictions.iLike(…)
]).list();

Thanks! The disjunction worked.

caseUsers = criteria.createAlias(“user”,“u”).disjunction([criteria.restrictions.ILIKE(“u.firstname”,"%#firstname#%"),criteria.restrictions.ILIKE(“u.lastname”,"%#lastname#%")]).list(sortOrder=“u.lastname ASC”,asQuery=false);