Relation Between Three Tables

Hi,

       I have three tables as follows

tbl_enquiry


| enquiry_id[pkey] | member_id[fkey] | product_id[fkey] | brand_id[fkey] |

tbl_member


| member_id[pkey] | citytown_id[fkey] | contact_no

tbl_citytown


|citytown_id[pkey] | city_town |

There relations are as follows

//Enquiry Model relations

‘member’=>array(self::BELONGS_TO, ‘TblMember’, ‘member_id’),

					'product'=>array(self::BELONGS_TO, 'TblProduct', 'product_id'),

//Member Model relations

‘citytown’=>array(self::BELONGS_TO, ‘TblCitytown’, ‘citytown_id’),

Now i want to update contact number and citytown within the enquiry view, but unable to show the citytown data in

enquiry and contact number is not updated. Please help me if i make any mistake as i am new in yii framework.

Thanks in advance.

Hi kapur, welcome to the forum.

You should be able to show the citytown data like this:




$enquiry = Enquiry::model()->find('...');

echo $enquiry->member->citytown->city_town;



You just have to cascade the relations.

Yii allows you to read the related models quite easily.

But, unfortunately, Yii doesn’t support the automated updating of the related models.




$enquiry = Enquiry::model()->find('...');

$enquiry->member->contact_no = 'something new';

$enquiry->save();

// this won't update the member's contact_no



You have to update the related models manually.

Good to know that, Thanks :)

Thanks softark for your reply.

I found another way to achieve it.

In model…

‘citytown’=>array(self::BELONGS_TO, ‘TblCitytown’, array(‘citytown_id’=>‘citytown_id’),‘through’=>‘member’),

In view…

$form->dropDownList($model->citytown, ‘citytown_id’, $listCity, array(‘empty’=>’(Select a City/Town)’));

This worked for generating the dropdown box.