Aaron
April 16, 2013, 6:57pm
1
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?
Aaron
April 16, 2013, 7:27pm
3
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()
Aaron
April 16, 2013, 7:38pm
5
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()
Aaron
April 17, 2013, 2:35pm
7
With that last snippet, didnt I do that?
gratzc
April 17, 2013, 2:44pm
8
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();
Aaron
April 17, 2013, 3:19pm
10
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);