2012-04-13 6 views
0

이상한 질문이 있지만 여기에 있습니다. 하나의 모델에 대해 여러 개의 레이블 배열을 설정하고 두 모델간에 전환하고 싶습니다. 내가 뭘해야 할 것은 :단일 모델의 다중 레이블 - Yii

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

이 가능 그렇다면 어떻게 할 때 사용되는 배열을 바꿀 것?

감사합니다.

답변

2

I하지 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

작품 .. 도움 주셔서 너무 감사 –