When we use Yii's Active Record methods, such as findAll
or find
, we can pass criteria as a parameter. It can be an array or an instance of the CDbCriteria
class. This class represents query criteria, such as conditions, ordering by, limit/offset, and so on.
Usually, the criteria
class is used as shown in the following example:
$criteria = new CDbCriteria(); $criteria->limit = 10; $criteria->order= 'id DESC'; $criteria->with = array('comments'); $criteria->compare('approved', 1); $criteria->addInCondition('id', array(4, 8, 15, 16, 23, 42)); $posts = Post::model()->findAll($criteria);
Internally, the criteria
class does not build any queries by itself, but it stores data and allows us to modify it. The actual work is being done inside the AR methods, where criteria are being used.
The preceding code can be read as follows:
Get 10 posts along with comments from approved posts with ID equals to 4, 8, 15, 16, 23, or 42 ordered by...