Get other attributes of the model

I have two tables admissions and the other one is decease.deceaseid is FK for admission and PK for decease.I used drop down list to show the decease list.

with that

<div class="row">


	<?php echo $form->labelEx($model,'decease_id'); ?>


	<?php echo $form->dropDownList($model,'decease_id',array('first'=>'Please select Decease',CHtml::listData(Decease::model()->findAll(), 'deceaseid', 'decease_name'))); ?>


	<?php echo $form->error($model,'decease_id'); ?>


</div>

when admisson form is opened list shows the name of the decease…and user select any of them…but when we saw database of adsmission table…it stored id of the decease rather than its name…and I want to store deceasae name in the database…The deceaseid field in the admission form is of integer type.So what should I do.plz HELP ME OUT.thanks in advance.

I believe the 2nd param (decease_id) in $form->dropDownList($model,‘decease_id’… should be the value that you’d like returned to your controller and hence the one that’ll be saved to your DB. Please correct me if I’m wrong. So in your case, it should be ‘decease_name’.

Cheers,

Matt

Sorry, I just noticed that your FK is of type integer in Admission table. Why are you trying to save a string to that field. Shouldn’t you be saving the Id of the deceased in that table? I don’t have all your details so please correct me.

Cheers,

Matt

I think your database schema is already correct. Just add a relation in your Admission model, for example:




'deceaseItem'=>array(self::HAS_ONE, 'Decease', 'deceaseid'),



And display it in view files, for example if displayed in CGridView:




array(

   'name'=>'deceaseid',

   'value'=>'$data->deceaseItem->decease_name',

),