Data Provider For List View

Hi, is it possible to use data providers of multiple models in a single list view?

in controller




public function actionData()

        {

           

            $dataProvider=new CActiveDataProvider('Test');

            $dataProvider2=new CActiveDataProvider('Address');

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

                            'dataProvider'=>$dataProvider,

                            'dataProvider2' => $dataProvider2,        

                            ));


        }



data.php





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

	'dataProvider'=>$dataProvider,

	'itemView'=>'view',

    'template'=>"{items}\n{pager}",  

));


?>


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

	'dataProvider'=>$dataProvider2,

	'itemView'=>'view',

    

));


?>






in view.php when i can display the result?

If you want the dataproviders in one CListView, you have to merge them. But if you just want to show two CListViews (everyone with an own dataprovider) on one view page, that’s no problem.

How it can merge?

I haven’t used such approach myself, but take a look at this wiki article.

But isn’t it possible in your case to create one provider right away? Usually if you need to render two models in one view they’re connected with some kind of relation, so you may just add call to related model in your criteria, similar to the example at CActiveDataProvider docs. This way you’ll eager load the other model data to be called in a form:


$model1->relationName->model2FieldName

Here’s the link to read more about relational queries, but probably you’re aware of such details ). So I’m mostly curious why eager/lazy loading can’t be used in your case and merging of data is needed.

Yuga