Writing a Criteria statement for the IN Operator

This is going to be a quick post as I needed to write this done as its quite useful for anyone who either doesnt like or doesnt understand how the complex criteria objects work.

If you want to write code for a criteria object for the SQL statement which is something like this.

SELECT field1, field2 from table1 where field1 in (5,30,23)

The corresponding PHP for that would be as follows

$values = array(5,30,23);
$criteria_for_select = new Criteria();
$c = new Criteria();
$criteria_for_select = $c->getNewCriterion(table1::field1, $values, Criteria::IN);

That’s pretty much it, now this criteria object $criteria_for_select can now be used in your doSelectXXX calls.

Another really cool app that automates the task of writing criteria object is availabile here.
http://propel.jondh.me.uk/

The cool thing about this utility is you drop in your selection condition and it will write the code for the criteria object for you, now that’s really cool.

About rp

Architect for large, highly scalable LAMP applications and Technical Manager with special focus on metrics based continuous improvement of teams and products. Rajat has close to a decade of experience of a very wide range of skills related to infrastructure, middleware, app servers all the way to front-end technologies and software development methodologies including agile, iterative waterfall, waterfall as well as ah-hoc startup using the right approach in the right context to reduce time to market.