yii db relationships with custom column names

I am bit confused about the yii db relationships. Here is snippet of my db schema:

master table 1:

campaigns (id, CID, campaign_name) -> model name is: Campaigns, CID is unique

master table 2:

affiliates (id, affid, affiliate_name, default_pixel) -> model name is: Affiliates, affid is unique

join table of above two master tables, need many-to-many relation:

campaigns_affiliates(CID, affid, tracking_pixel) -> model name is: CampaignsAffiliates

From above schema I need help with two things:

  1. Suggest the relation that should be defined at each Model level

  2. MVC structure suggestion for Join table, since join table has extra column of tracking_pixel to override default_pixel defined in affiliates table, should it have its own different controller or should be handled by either AffiliatesController or CampaignsController

Any suggestions?

As far as the relations, have you read and understood Relational Active Record?

There are countless posts about how to work with Many-to-Many relationships in different ways around here. Personally I utilize the giix extension from mentel. If you haven’t already I would recommend taking a look at that.

I am fine with the general buid of AR relations but its pretty general and obviously doesnt cover the complex scenarios. Also tried to search a lot but didnt get much help from other threads or blogs.

Wasn’t aware of giix, thanks for sharing it. I will try it and drop in my experience with it.

There is an example in there that looks like it will cover what you want.

Well somehow i managed to address the issue by violating lots of MVC and AR rules.

However my questions still stands, in rails we have provision to specify :primary_key parameter in relationship defintion, yii doesnt have any such facility?

In the table definition that I used:

campaigns (id, CID, campaign_name) -> model name is: Campaigns, CID is unique and id as primary key.

campaigns_affiliates(CID, affid, tracking_pixel) -> model name as CampaignsAffiliates

each CampaignsAffiliates object BELONGS_TO Campaigns and I wanted to access the relationship as:

$campaign = CampaignsAffiliates::model()->findByAttributes(array("CID"=>"23"))->campaign

so that it would allow me to use $campaign->campaign_name directly.

But seems there is now way in YII to set this kind of relationship where primary key is not "id". Field CID from campaigns_affilaites was pointing to "id" of campaigns which was wrong in my case.

I am not sure if I used it correctly or did not find a way to address this problem. If someone knows how to address it would really appriciate if you can write down the exact relation code that I should had used in the Model Classes of both tables.