Web Development

I have always been fascinated about the Yii Framework. The sheer number of classes you can work with blows me away.

One such Yii Framework class I have been with is CDbCriteria. The CDbCriteria class makes it easier to work with CActiveRecord.

CDbCriteria has a lot of useful methods:

Name Repository
select columns
condition conditions with parameters
params conditions with parameter placeholders
group how to group results
join how to be joining other tables
limit Sets the limit on the records
order Sets the order of records
offset Sets the offset

Here are a few uses for CDbCriteria:

Find By Primary Key

Finding without formally using CDbCriteria can be easily done via CActiveRecord’s findByPk method:

$post = Post::model()->findByPk(1);

When using CDbCriteria, you can rewrite it like so:

$criteria = new CDbCriteria;
$criteria->condition = 'id=:id'
$criteria->params = array(':id' => 1)
$post = Post::model()->find($criteria);

It’s a little more verbose :)

Searching Records

Searching is a basic task with database applications. CDbCriteria makes it a little more fun:

$criteria=new CDbCriteria;
$criteria->addSearchCondition('title', $keyword);