Hey community,
I have 2 questions concerning the CActiveDataProvider:
-
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).
-
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