Outputing the whole relation query result

Hi,

I’m new to yii and I’m having this problem here.

I have 3 tables: News, NewsTags, Tags.

News are related to Tags through NewsTags, which contains a ntg_nws_id and ntg_tag_id.

News




return array(

   'nwstags' => array(self::BELONGS_TO, 'NewsTags', 'nws_id'),

);



NewsTags




return array(

   'nwstags' => array(self::HAS_MANY, 'News', 'ntg_nws_id'),

   'all_tags' => array(self::BELONGS_TO, 'Tags', 'ntg_tag_id'),

);



Tags




return array(

   'all_tags' => array(self::HAS_MANY, 'NewsTags', 'tag_id'),

);



The News table is outputed in the CGridView, and what I want to do is to output all the tags related to this news article. I do the




'name' => 'nwstags.all_tags.tag_name'



But it only outputs 1 result per news article.

Help please! :)

Looks like you have a many-many relationship between News and Tags. So you might want to change like this

News




return array(

  'tags' => array(self::MANY_MANY, 'Tag', 'tbl_news_tags(nws_id, tag_id)')

);



and in the Tags module?

In the view, do I access it now like ‘tags.tag_name’?

sorry, I’m just a real noob about the yii relations )))

you need to make tbl_news_tags table for relate news and tags tables. You can read manual page about it.

Also you can see this topic