Last Updated: February 25, 2016
·
2.003K
· kachar

Yii CActiveDataProvider with scopes

If you ever wondered how to restrict the data provider to a given scope here's the easiest way:

public function actionIndex()
{
    $dataProvider=new CActiveDataProvider('Orders');
    $this->render('index',array(
        'dataProvider'=>$dataProvider,
    ));
}

becomes:

public function actionIndex()
{
    $model = Orders::model()->own()->published();
    $dataProvider=new CActiveDataProvider($model);
    $this->render('index',array(
        'dataProvider'=>$dataProvider,
    ));
}

The CActiveDataProvider class accepts ActiveRecord object as first parameter in the constructor.