Label changes depending on scenario

I have a model that I use for two different but similar uses. The model is used for job opportunities and for students to post a brief description of themselves and qualifications. Rather than make two different tables and two different models, I just added a field to indicate that the posting was a student post (type = 0) or employer post (type = 1).

I create the two posts in two different controller functions

public function actionStudentPost()


		$model=new Posting('student');

                $model->type = 0;

                $model->location ="I am willing to re-locate";


$model=new Posting('employer');

                $model->type = 1;

                $model->location ="Various";

So I will be creating a ‘Posting’ with scenario of ‘student or employer’ two of the field values are already defined when the application displays the form but I also want to change the labels on the form rather than call two different forms.

In my model I check the scenario and assign labels.

public function attributeLabels()



             case 'student':

             return array(

			'id' => 'ID',

			'display' => 'Display',

			'type' => 'Type',

			'creationDate' => 'Creation Date',

			'updateDate' => 'Update Date',

			'area' => 'My area of interest/expertise',

                        'title'=>'Name and Education',

			'details' =>'Some information about me',

			'location' =>'This is where I would like to live',

			'email' => 'My Contact Email',



             default: // employers end up here

             return array(

			'id' => 'ID',

			'display' => 'Display',

			'type' => 'Type',

			'creationDate' => 'Creation Date',

			'updateDate' => 'Update Date',

			'area' => 'Area of expertise required',

                        'title'=>'Breif description of opportunity',

			'details' =>'Opportunity details',

			'location' =>'Geographic location of opportunity',

			'email' => 'Contact Email',





This all works perfectly but is it a hack? is there a better way to use Yii when these are the desired results?

I have found a few other posts that ask similar questions but no answers were provided is the scenario ‘ON’ only used in the rules?


This is fine, and it is one of the reasons why we use attributeLabels() instead of $attributeLabels because the former allows you to dynamically return the needed labels. You may treat scenario as a state of the model instance, based on which you can do things besides validations.

OK thanks, so in my view (_form) the following code dumps out the array of attribute labels based on my current scenario which is being specified in the controller.


And I understand that the attributeLabels() method is returning the values defined within.

Is there any way to change the labels on the fly?


$model->attributes->label('location') = 'This is my planet of choice';

FWIW I’m only asking if you can do this not that I think it is something that we should be able to do although it could help in the case of EAV tables for example.


This solution is what i was looking for. For me this is however not working (Yii 1.1.14). I create the model in my controller

// Controller

$model = new Arbeiten("myScenario");

When i output the scenario in my view it is displayed as i wished

// view

echo $model->scenario // myScenario

When i try to ask in my model for the scenario, there is nothing

public function attributeLabels() {


... // string(0)

Did i miss something? Did anything change so i cannot check the scenario in the model any more?

Thanks for any help!

check this wiki

i hope it’s some help.

I think it’s just because ‘scenario’ can be empty for some occasions. :)

Thanks for the Hints. I found the solution: I used Giix, and with this extions the solution above is not working!

I’ve just came across the same problem as well. GxActiveRecord overrides getAttributeLabel() method with getRelationLabel() which uses static model of the class thus the scenario is empty.

To solve this I used the original CActiveRecord::getAttributeLabel method in the model.

public function getAttributeLabel($attribute) {

	return CActiveRecord::getAttributeLabel($attribute);