2

我想缓存 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']),
            )
        ));

但无济于事:查询没有被缓存。

4

1 回答 1

1

如果您使用分页,则需要将缓存queryCount属性增加到 2,前一个查询用于计数(用于分页),后者用于获取数据。

将您的代码更改为:

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

更多的

于 2014-01-12T09:23:29.930 回答