奇怪的问题,但在这里。我想为一个模型设置多个标签数组,然后在它们之间切换。 我需要的是:
public function attributeLabels_1(){
array(
'line_1'=>'Authentication Number'
)
}
public function attributeLabels_2(){
array(
'line_1'=>'Receipt Number'
)
}
这可能吗?如果可以的话,你怎么改变使用哪个数组?
非常感谢。
答案 0 :(得分:2)
我不记得attributeLabels()
返回的列表是否缓存在某处,如果不是,那么这应该有效:
/** implementation */
private $_currentLabelCollection = null;
public function getCurrentLabelCollection() {
return $this->_currentLabelCollection;
}
public function setCurrentLabelCollection($value) {
if(!$value || array_key_exists($value, $this->_attributeLabelCollections)) {
$this->_currentLabelCollection = $value;
} else {
throw new CException(Yii::t("error", "Model {model} does not have a label collection named {key}.", array(
'{model}' => get_class($this),
'{key}' => $value,
)));
}
}
private $_attributeLabelCollections = array(
'collection1' => array(
'line_1' => 'Authentication Number',
),
'collection2' => array(
'line_1' => 'Receipt Number',
),
);
public function attributeLabels() {
if($this->_currentLabelCollection) {
return $this->_attributeLabelCollections[$this->_currentLabelCollection];
} else {
return reset($this->_attributeLabelCollections);
}
}
/** usage */
// use labels from 'collection2'
$model->currentLabelCollection = 'collection2';
// use labels from the first defined collection
$model->currentLabelCollection = null;