0

Odd question but here it goes. I would like to set up multiple arrays of labels for one model and then switch between them. What i need is:

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

Is this possible and if so how would you change which array is used when?

Many thanks.

4

1 回答 1

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;
于 2012-04-13T05:41:51.503 回答