OK, I get this now:
Gii generates the following (where dataprovider is generated in the controller):
Controller:
$dataProvider=new CActiveDataProvider('modelName');
/* Here you add all your filtering criteria and sorting and paging, etc. */
$this->render('index', array('dataProvider'=>$dataProvider));
View:
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
)); ?>
However, this setup is not very good because the dataProvider is generated in the controller (probably just to give us an example). But it means that you have to duplicate your dataProvider-generation-code in every controller or every method where you need it.
To generate the dataprovider in the model, you have to change things like this:
Controller:
$model=new modelName;
$this->render('index', array('model'=>$model));
View:
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$model->search(), /* the same as CGridView */
));
Now, all the filtering, sorting and paging is done in the model’s search function.
Example of re-using the search() function for CGridViews and CListViews:
We know that CDetailView is not very powerful if you want to display information from a model with lots of related information that also needs to be displayed.
But now you can replace CDetailView with CListView (which is powerful due to html tables and php forEach() methods) and simply display only one record’s information.
Controller:
$pk = primaryKeyOfRecordToDisplay
$model=new modelName;
$this->render('index', array('model'=>$model, 'pk'=>$pk));
View:
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$model->search($pk), /* $pk is passed to search() function */
));
In your model, change the search() function as follows ( or if you want, create a new function to be used by CListView):
Public function search($pk=null) /* If $pk was not passed by CGridView or CListView, it defaults to null. */
{
$criteria=new CDbCriteria;
If ($pk !== null) {
$criteria->compare('t.pk', $pk, false); /* if $pk was passed, then filter on it */
}
... more filtering and sorting criteria ...
}
Now, if $pk was passed by any CGridView or CListView, then search() will only return that one record. If $pk was not passed, then search() will return all records.
Obviously you can have CListView now displaying a single record or all the records or whatever you filtered on.
Thanks softark.