Search code examples
phpyii

Yii inherit attributeLabels


With Yii php framework, I use inheritance.

In my AbstractModel, I have this method:

public function attributeLabels()
    {
        return array(
            '0'=>Yii::t('default','No'),
            '1'=>Yii::t('default','Yes'),
        );
    } 

In my object who extends AbstractModel, I have this method:

public function attributeLabels()
    {
        return array(
            'username' => Yii::t('user', 'email'),

        );
    }

In a view file, I use:

<?php echo CHtml::activeLabel($model, $model->property);?>

But I never show 'No' or 'Yes' from asbtractModel. If I put all in my model it works. But I want to use inheritance. How can I concat parent attributeLabels with current model attributeLabels?


Solution

  • Simply merge the return value of the parent method in MyObject (model class):

      public function attributeLabels() {
        return array_merge(
          parent::attributeLabels(),
          array(
            'username' => Yii::t('user', 'email'),
          )
        );
      }
    

    You may also use CMap::mergeArray().