0

I'm having some trouble trying to pass multiple dataProviders to a ClistView in Yii. What is the correct way of doing it?

I've tried:

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


'dataProvider'=>array($dataProvider, $dataProvider2),

I've also had a look at this What is the best method to merge two PHP objects?. Not sure if the best approach would be to try and do it with pure PHP or if the Framework has options for this and i've just missed them.

Appreciate any help.

thanks

Jonny

Community
  • 1
  • 1
Jonnny
  • 4,939
  • 11
  • 63
  • 93
  • 2
    By design you can only pass one data provider to a ListView or GridView. Anything else doesn't make much sense. You should better try to get the merged data from your provider. – Michael Härtl Aug 26 '13 at 20:33
  • @MichaelHärtl Thanks Michael, you help me a lot. If you want to put this as the answer I shall mark accept it. – Jonnny Aug 27 '13 at 02:45

2 Answers2

2

The easiest thing is if you have a relation between the two models. Then you can just output it as is in your itemView:

echo $data->relationItem; 

// Or if it is a HAS_MANY relation
foreach ($data->relationItems as $i)
    echo $i->something;

Not what you were after?

Another solution could be to iterate through your models, cherry-pick the things you want into a new array and then feed it using a CArrayDataProvider to your ClistView.

ippi
  • 9,857
  • 2
  • 39
  • 50
  • Thanks Ippi. It's not quite what I'm after, these are two very different CSqlDataProvider models. I haven't seen any real answers to this so I think Michael probably summed it up the best. But thanks for this, it may be helpful to me and others. – Jonnny Aug 27 '13 at 02:48
1

In situation like this, I develop my own CListView:

class MyClistView extends CListView{

}

the dataProvider is required for CListView so pass ArrayDataProvider that contains your dataproviders and Iterate them and dont forget to implement your logic for rendering.

PS: Dont forget to take a look to CBaseListView

Cherif BOUCHELAGHEM
  • 1,126
  • 2
  • 14
  • 21