When to use CActiveDataProvider?

Hey community,

I have 2 questions concerning the CActiveDataProvider:

  1. When to use CActiveDataProvider and when a model? Both (e.g. admin and index actions) just deliver the db content based on ActiveRecords (like documentation explains).

  2. I tried to apply criteria to both gii-generated actions attached below. It works, but did I do it the way it’s meant to be done?

The gii-generated index works with CActiveDataProvider

index action:


public function actionIndex()

	{

        $criteria=new CDbCriteria;


        $criteria->addCondition('topic LIKE "%castle%"');


		$dataProvider=new CActiveDataProvider('Grade', array(

			'criteria'=>$criteria,

        ));

		$this->render('index',array(

			'dataProvider'=>$dataProvider,

		));

	}



index view:


$this->widget('zii.widgets.CListView', array(

	'dataProvider'=>$dataProvider,

	'itemView'=>'_view',

));

whereas the admin action uses a model

admin action:


	public function actionAdmin()

	{

		$model=new Grade('search');

		$model->unsetAttributes();  // clear any default values

		

                $model->topic="castle";


		if(isset($_GET['Grade']))

			$model->attributes=$_GET['Grade'];

		$this->render('admin',array(

			'model'=>$model,

		));

	}



admin view:


$this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'grade-grid',

	'dataProvider'=>$model->search(),

...

Thank you very much for your support.

Steffen

CActiveDataProvider is actually a "level above" a model. Its mainly used with list views like CGridView. It contains (or better: provides) a list of models (data) that represent a single page in a larger resultset. It also contains the respective pagination and sort objects.

Mike, thank you for your explanation.