Php 如何更改Yii dropDownList和Cgridview中模型属性的显示值?
我正在使用PHPYII并尝试显示从数据库中保存的值派生的值 这是我的模型 模型贸易记录 视图索引 如您所见,我在模型中为映射关系设置了一个getTradetype方法。 我试图使代码干净。但是我认为对于dropdownlist可能有更好的解决方案。至于Cgridview,我的代码根本不起作用。Php 如何更改Yii dropDownList和Cgridview中模型属性的显示值?,php,yii,Php,Yii,我正在使用PHPYII并尝试显示从数据库中保存的值派生的值 这是我的模型 模型贸易记录 视图索引 如您所见,我在模型中为映射关系设置了一个getTradetype方法。 我试图使代码干净。但是我认为对于dropdownlist可能有更好的解决方案。至于Cgridview,我的代码根本不起作用。 谢谢。对于网格视图,请将函数更改为返回字符串,而不是数组 public function getTradetype(){ $types = array('1' => 'Buy', '2' =
谢谢。对于网格视图,请将函数更改为返回字符串,而不是数组
public function getTradetype(){
$types = array('1' => 'Buy', '2' => 'Sell');
return isset($types[ $this->type ]) ? $types[ $this->type ] : 'undefined';
}
如果您希望在标题中指定变量的默认名称,则不需要在数组中指定它,值和名称就足够了,我认为您没有正确设置名称作为替代,我通常将关系作为数组,并使用类似于他/她的答案的函数
public static $tradeTypes = array('1' => 'Buy', '2' => 'Sell');
...
public function getTradeType() {
return isset(self::$tradeTypes[ $this->type ]) ? self::$tradeTypes[ $this->type ] : 'undefined';
}
这使得可以使用TradeRecord::tradeTypes作为下拉列表的选项,使用tradeType作为网格视图的值
public function getTradetype(){
$types = array('1' => 'Buy', '2' => 'Sell');
return isset($types[ $this->type ]) ? $types[ $this->type ] : 'undefined';
}
public static $tradeTypes = array('1' => 'Buy', '2' => 'Sell');
...
public function getTradeType() {
return isset(self::$tradeTypes[ $this->type ]) ? self::$tradeTypes[ $this->type ] : 'undefined';
}