Select extra column from Many Many table

I have a Many to Many table with an extra column (the time that the record has been added):


companyId | userId | addTime

In my User model i have:

'memberCompanies' => array(self::MANY_MANY, 'Company', 'user_company(userId, companyId)', 'together' => true)

To get all users linked to a company i use:


$criteria = new CDbCriteria;

$criteria->with = 'memberCompanies';

$criteria->compare('memberCompanies_memberCompanies.companyId', $this->id);

return new CActiveDataProvider('User', array(

       'criteria' => $criteria




But how can i pass the ‘addTime’ to my gridview?

Dividing the relation to 2 relations might be a decent solution … it seems somewhat stupid, but I can’t think of a smart solution.

I mean …


User - MANY_MANY - Company


User - HAS_MANY - UserCompany

UserCompany - BELONGS_TO - Company

Perfect solution.

What you have is really not a many_many relationship if you have extra data columns. You really have two has_many relationships so what softark is proposing is correct.

If you feel like searching there is a good topic at StackOverFlow about this same issue (its not a yii topic but a database design one).