Yii如何在CActiveDataProvider中缓存

时间:2014-01-11 11:38:36

标签: caching yii cactivedataprovider

我想缓存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 个答案:

答案 0 :(得分:1)

如果您使用分页,则需要将缓存 queryCount 属性设置为2,以前的查询用于计数(用于分页),后者用于获取数据。

将您的代码更改为:

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

more