Display User Models In Clistview

Suppose I have the following:

User model relation:

'listings' => array(self::HAS_MANY, 'Listing', 'user_id'),

In my ListingController I want to display a CListView with the above relation. So basically it will display all listing by this user.

How can I do this? The CListView needs a CActiveDataProvider to work. By the way I use Yii::app()->user->id to get the user ID.

No. [font=“Courier New”]CListView[/font] needs a class implementing [font=“Courier New”]IDataProvier[/font]. So any DataProvider is sufficient :)

Your easy way out is to pass [font="Courier New"]$user->listings[/font] to a CArrayDataProvider.

The cleaner solution were to set up a CActiveDataProvider for the [font="Courier New"]Listing[/font] class and filter out by [font="Courier New"]user_id[/font]:

<?php echo $this->widget('CListView', array(

  'dataProvider'=>new CActiveDataProvider('Listing', array(










Thanks. That works well. I was just hoping to avoid code repetition…

Well, that’s CArrayDataProvider for you. But you will end up with no (easy) filtering capabilities and sorting in PHP userland (which you usually want to avoid).