2012-04-13 18 views
0

奇怪的問題,但在這裏。我想爲一個模型設置多個標籤數組,然後在它們之間切換。 我需要的是:單個模型中的多個標籤 - Yii

public function attributeLabels_1(){ 
    array(
      'line_1'=>'Authentication Number' 
    ) 
} 
public function attributeLabels_2(){ 
    array(
      'line_1'=>'Receipt Number' 
    ) 
} 

這是可能的,如果可以,如何改變其陣列時使用?

非常感謝。

回答

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; 
+0

完美的作品。謝謝你這麼多的幫助 – 2012-04-14 16:53:17