Botir Ziyatov
Botir Ziyatov

Reputation: 518

Yii How to Cache in CActiveDataProvider

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.

Upvotes: 2

Views: 1216

Answers (1)

msoa
msoa

Reputation: 1349

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

Upvotes: 1

Related Questions