Archive for March, 2013
If you use ColdBox’s awesome CriteriaBuilder at all, you know it’s dead simple to create complex AND dynamic criteria queries. If you’re like me, a lot of your criteria queries are a bunch of “and”s built together. For example, let’s imagine we’re trying to get all the cars from our database that have a make of Ford and a color of Red. A simple criteria query for this might look like:
// create criteria var c = newCriteria(); // add criteria c.isEq( "Make", "Ford" ) .isEq( "Color", "Red" ); // get result var result = c.list();
Pretty simple, right?
Of course, not all queries are “and“s. Sometimes, you need to do an “or“. Perhaps we want to get all the cars that are Ford OR Red. Again, this is very easy with CriteriaBuilder:
// create criteria var c = newCriteria(); // add criteria c.or( c.restrictions.isEq( "Make", "Ford" ), c.restrictions.isEq( "Color", "Red" ) ); // get result var result = c.list();
While this works, it’s not complete. Sure, there are some scenarios where you will know ahead of time what criteria you want to use in a criteria query. However, consider a scenario in which you have a search form in which a user can select from a list More >