Using Switch on data for CGridView

I am trying to replace data in my grid view, but I can seem to get it to work. My data field ‘userroles’ is set as numerical Values, in my code I would like to replace the values with labels to represent the data.

I created a method in the controller call getValues()




	public function getValues($id){

	    switch ($id) {

           case 1: $role = "'User'"; break;

           case 2: $role = "'Tech'"; break;

           case 3: $role = "'SuperTech'"; break;

           case 4: $role = "'Admin'"; break;

		   break;

		   default: $role = "'No Role Selected'"; break;

		}

		return $role;

	}



And in my Grid View I have…




		array(

			'name' => 'userRoles',

			'value' => $this->getValues($model->userRoles),

			),

		//'admin_disabled',



If I change return $roles to return $id I get the correct Values back, So I know I am passing the correct values. All I get back is No Role Selected.

A better approach might be, to but this right into your model:




public function getUserRoleLabel()

{

    switch($this->userRoles) {

         // return label here

    }

}

Then in your gridview you just can use ‘userRoleLabel’ as if it was a real attribute of your model.

… oh, and then the plural implies, that userRoles is an array of ids? How should that work then? You only compare against a single value in your switch.

So what exactly is userRoles and what do you want to output in the end?