Common Cgridview Tasks

Last modified: 
Sunday, March 29th, 2015

Overview

Common tasks and adjustments when working with Yii's CGridView and CDbCriteria.

Limit Result Set Based on a Condition

The following addition to MyModel.php will present a searchable gridview which includes only items where
Model.active = 1.

public function search() {

  $criteria=new CDbCriteria;

  $criteria->condition = 'active = 1';  
  $criteria->compare('id',$this->id);
  $criteria->compare('url',$this->url,true);
  $criteria->compare('active',$this->active);

  return new CActiveDataProvider($this, array(
    'pagination' => array(
      'pageSize' => 50,
    ),  
    'criteria'=>$criteria,
  ));
}


The operator of this site makes no claims, promises, or guarantees of the accuracy, completeness, originality, uniqueness, or even general adequacy of the contents herein and expressly disclaims liability for errors and omissions in the contents of this website.