【发布时间】:2014-06-03 02:48:04
【问题描述】:
我正在使用 PHP Yii 并尝试显示从保存在数据库中的值派生的值。
这是我的模型
模型交易记录
public type; //Type:'1' means Buy,'2' means Sell. $model->type is get from database
public function attributeLabels(){
return array(
/* some attribute */
'type'=>'Trade Type' //This is also the column header
}
public function getTradetype(){
return array('1' => 'Buy', '2' => 'Sell');
}
查看-索引
<!-- dropdown list-->
<?php echo CHtml::dropDownList('TradeRecord[type]', $model->type, //any better solution?
$model->tradetype,
array('empty' => '(select..)','class'=>'form-control'));
?>
<!--CgridView column-->
<?php $this->widget('bootstrap.widgets.BsGridView', array(
'id'=>'trade-record-grid',
'dataProvider'=>$dp,
'columns'=>array(
array(
'header' => '$data->getAttributeLabel("type")', //not worked!
'name'=>'type',
'value'=>'($data->tradetype($data->type))', //not worked!
),
),
如您所见,我在模型中为映射关系设置了一个 getTradetype 方法。 我试图使代码干净。但是我认为对于下拉列表的情况可能有更好的解决方案。至于 Cgridview 的情况,我的代码根本不起作用。 谢谢。
【问题讨论】: