Many To Many Relation In Cgridview

Advertiser model:


/**

 * @return array relational rules.

 */

public function relations()

{

	// NOTE: you may need to adjust the relation name and the related

	// class name for the relations automatically generated below.

	return array(

	  'users' => array(self::MANY_MANY, 'User', 'user_advertiser(uid,aid)'),

    'campaigns' => array(self::HAS_MANY, 'Campaign', 'aid'),

	);

}

User model:


/**

 * @return array relational rules.

 */

public function relations()

{

	// NOTE: you may need to adjust the relation name and the related

	// class name for the relations automatically generated below.

	return array(

	  'assignments' => array(self::HAS_MANY, 'Assignments', 'userid'),

	  'advertisers' => array(self::MANY_MANY, 'Advertiser', 'user_advertiser(uid,aid)'),

	);

}



This is a many to many relationship that is tied in by the user_advertiser table. In my CGridView I want to list the advertiser name, user first name and user last name with role of “sales rep” (fist name and last name are separate fields). I want to be able to sort and also filter by the sale rep first and last name. Anyone have an example of a many to many relationship like mine implemented. I looked at this article but it didn’t work for me.

Try http://www.yiiframework.com/extension/relatedsearchbehavior/

Could you please share your code from controller file and view file also.

Hi

Check out this Dynamic Parent Child CGridViews wiki.

It has related data in a single CgridView (which is what I think you want), but it also shows how to sort/filter/edit/delete the related records in their own CGridView after clicking in the parent CGridView.

Check this out, I had same problem, however I worke with Postgresql and it’s a bit different than MySQL. Therefore it doesn’t cost much to try :

http://www.yiiframework.com/forum/index.php/topic/44838-filter-and-sort-a-column-fed-by-a-many-many-relation/page__p__212356__fromsearch__1#entry212356