A related hyperlink column in CGridView, CDetailView, CListView
Published on 1/6/2011
Say you have an order table and it has a status id. You want to display the status name and not the id, as a hyperlink, that links to the status view page, where you can see other info about that status. Here’s how to do it:
relation name: status_relation
id field: status_id
name field: name
related controller name: status_controller
label: Status
In your ‘columns’ array in a CGridView declaration:
array('name'=>'status_id', 'type'=>'raw', 'value'=>'CHtml::link($data->status_relation->name, array("status_controller/view", "id"=>$data->status_id))', 'header'=>'Status'),
watch your single and double quotes
$data is the name of the model record when inside a CGridView
This method allows for searching and sorting, as long as you adjust your model’s search function as per the previous post.
In your ‘attributes’ array in your CDetailView:
array('label'=>'Status', 'type'=>'raw', 'value'=>CHtml::link($model->status_relation->name, array('status_controller/view','id'=>$model->status_id))),
In the _view of your CListView:
CHtml::link(CHtml::encode($data->status->name), array('status_contoller/view', 'id'=>$data->status_id));
... views