For my app I have a table with Genders (should only be two I hope) but I have three because I am porting data from a large database and many entries don’t have the gender specified. So I have
CREATE TABLE IF NOT EXISTS `Gender` (
`id` int(1) NOT NULL auto_increment,
`name` char(25) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin ;
-- entries
1=>'male',
2=>'female',
3=>'unspecified'
This is linked to my participants table with participant.gender_id --> gender.id
Participant model
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(
'donations' => array(self::HAS_MANY, 'Donation', 'participant_id'),
'eventParticipants' => array(self::HAS_MANY, 'EventParticipant', 'participant_id'),
'events' => array(self::HAS_MANY, 'Events', 'contact_id'),
------>'gender' => array(self::BELONGS_TO, 'Gender', 'gender_id'),
'lodgingtype' => array(self::BELONGS_TO, 'LodgingType', 'lodgingtype_id'),
'participantCreditCards' => array(self::HAS_MANY, 'ParticipantCreditCard', 'participant_id'),
'participantFoodHealths' => array(self::HAS_MANY, 'ParticipantFoodHealth', 'participant_id'),
'participantParticipantLevels' => array(self::HAS_MANY, 'ParticipantParticipantLevel', 'participant_id'),
'participantPrograms' => array(self::HAS_MANY, 'ParticipantProgram', 'participant_id'),
);
}
Now in controller (no change from previous example)
$dataProvider=new CActiveDataProvider('Participant', array(
'pagination'=>array(
// every property of CPagination can be configured here
'pageSize'=>self::PAGE_SIZE),
));
$this->render('list',array('dataProvider'=>$dataProvider));
}
Now in view
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'pager'=>array('maxButtonCount'=>5),
'columns'=>array(
array( // display a column with "view", "update" and "delete" buttons
'class'=>'CRudColumn',
),
array( 'class'=>'CLinkColumn',
'label'=>'View',
'urlExpression'=>'Yii::app()->createUrl("participant/show",array("id"=>$data->primaryKey))'),
'firstname',
'lastname',
'gender_id', <------- this shows the gender id#
'phone1'
),
));
This now shows the gender id (1, 2, or 3 in my case)
To see the name of the gender (more useful) eg, Male, Female, unspecified.
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'pager'=>array('maxButtonCount'=>5),
'columns'=>array(
array( // display a column with "view", "update" and "delete" buttons
'class'=>'CRudColumn',
),
array( 'class'=>'CLinkColumn',
'label'=>'View',
'urlExpression'=>'Yii::app()->createUrl("participant/show",array("id"=>$data->primaryKey))'),
'firstname',
'lastname',
'gender.name', <------ now it shows the name of the gender
'phone1'
),
));
This works!
doodle