Yii How to Cache in CActiveDataProvider

1.2k Views Asked by At

I want to cache the DB results for a CActiveDataProvider instance.

that no caches CActiveDataProvider? or all of it works fine?

I prepare the data provider in a controller's action, and then I use the data provider in a CGridView, later in some view.

this my code:

    $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']),
            )
        ));

but to no avail: the query is not being cached.

1

There are 1 best solutions below

0
On

If you using pagination you will need increase the cache queryCount property to 2, former query is for counting(used in pagination) and latter for fetching data.

Change your code to:

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

more