2014-01-11 36 views
2

我想緩存CActiveDataProvider實例的DB結果。Yii如何在CActiveDataProvider中緩存

那沒有緩存CActiveDataProvider?或所有的工作正常?

我在控制器的動作中準備數據提供者,然後在一些視圖中稍後在CGridView中使用數據提供者。

這是我的代碼:

$criteria = new CDbCriteria; 
    $criteria->with = array('person', 'department', 'position', 'policy'); 
    $criteria->condition='t.companyID=:companyID AND t.state = :state'; 
    $criteria->params = array(':companyID'=>$companyID, ':state'=>VersionedActiveRecordState::ACTIVE); 
$dataProvider = new CActiveDataProvider(Employee::model()->cache(2000,null), array(
      'criteria' => $criteria, 
      'totalItemCount'=>200, 
      'sort' => array(
       'defaultOrder' => 'person.lastName ASC, person.firstName ASC, person.patronymic ASC', 
       'attributes' => array(
        'ID', 
        'code', 
        'grade', 
        'employeeFullName' => array(
         'asc' => 'person.lastName ASC, person.firstName ASC, person.patronymic ASC', 
         'desc' => 'person.lastName DESC, person.firstName DESC, person.patronymic DESC', 
        ), 
        'departmentTitle' => array(
         'asc' => 'department.title ASC', 
         'desc' => 'department.title DESC', 
        ), 
        'positionTitle' => array(
         'asc' => 'position.title ASC', 
         'desc' => 'position.title DESC', 
        ), 
        'scheduleTitle' => array(
         'asc' => 'schedule.title ASC', 
         'desc' => 'schedule.title DESC', 
        ), 
        'policyTitle' => array(
         'asc' => 'policy.title ASC', 
         'desc' => 'policy.title DESC', 
        ), 
       ), 
      ), 
      'pagination' => array(
       'pageSize'=> Yii::app()->user->getState('employee_pageSize',Yii::app()->params['defaultPageSize']), 
      ) 
     )); 

但無濟於事:查詢不被緩存。

回答

1

如果您在使用分頁你將需要增加緩存queryCount屬性爲2,前查詢計數(在分頁使用)和後者爲獲取數據。

你的代碼更改爲:

$dataProvider = new CActiveDataProvider(Employee::model()->cache(2000,null,2) 

more

相關問題