display values of MANY MANY relation in model view with gridview or list

Hi,

How can I display many many relation as a grid or list.

Scenario:

class Post extends CActiveRecord

{

public function relations()


{


    return array(


        'author'=>array(self::BELONGS_TO, 'User', 'author_id'),


        'categories'=>array(self::MANY_MANY, 'Category',


            'tbl_post_category(post_id, category_id)'),


    );


}

}

In view.php of th "Post" model

How can I display $post->categories using a widget (CList or GridView)?

I can render sub view with foreach $post->categories as $category (and echo $category->name)

but I would like to use a widget.

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

'dataProvider'=&gt; <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?,


'itemView'=&gt;'_view',

)); ?>

I am assuming that tbl_post_category() is a join table between Post and Catagory without a Primary Key.

If so:




//Post model

public function relations()

{

    return array(

        'author'=>array(self::BELONGS_TO, 'User', 'author_id'),

        'tbl_post_category'=>array(self::HAS_MANY, 'Tbl_post_category', array('ID'=>'postID'),

        'categories'=>array(self::HAS_MANY, 'Category',  array('catergoryID'=>'ID'), 'through'=>'tbl_post_category'),

    );

}






//Catagory Model

public function relations()

{

    return array(

        'tbl_post_category'=>array(self::HAS_MANY, 'Tbl_post_category', array('ID'=>'categoryID'),

        'posts'=>array(self::HAS_MANY, 'Post',  array('postID'=>'ID'), 'through'=>'tbl_post_category'),

    );

}



Since the Tbl_post_category model does not have a Primary Key but uses a composite key(this will also help you to locate and remove a post from a catergory and visa versa):




//Tbl_post_category model

public function primaryKey(){

    return array('postID', 'catergoryID');

}



So to display all the Catergory items for a post:




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

    'dataProvider'=> new CArrayDataProvider($model->catergories, array('keyField'=>'ID')),,

    'columns'=>array(

        'catergory',

    ),

)); ?> 



Hope This helps.