Displaying model function output in a CGridView

I need some help calling a function in CGridView of the model being displayed, that displays the text/message/name related to a number:

I have a ‘User’ model that has an attribute called ‘status’. This is an integer field, and the numbers indicate a status. I also have the following functions (copied from the blog tutorial) in the model to convert the status numbers to messages:


    public function getStatusOptions()

    {

        return array(

            self::STATUS_UNCONFIRMED=>yii::t('core','Unconfirmed'),

            self::STATUS_ACTIVE=>yii::t('core','Active'),

            self::STATUS_DISABLED=>yii::t('core','Disabled'),

            self::STATUS_UNREGISTERED=>yii::t('core','Unregistered'),

        );

    }


    public function getStatusText()

    {

        $options=$this->statusOptions;

        return isset($options[$this->status]) ? $options[$this->status] : yii::t('core','Unknown');

    }

I have declared the following action in the User controller:


class AdminAction extends CAction

{

	const PAGE_SIZE=10;


	public function run()

	{

		$dataProvider=new CActiveDataProvider('User', array(

			'pagination'=>array(

				'pageSize'=>self::PAGE_SIZE,

			),

		));


		$this->controller->render('admin',array(

			'dataProvider'=>$dataProvider,

		));

	}

}

The user/admin view contains the following (crud generated) CGridView:


<?php $this->widget('zii.widgets.grid.CGridView', array(

    'dataProvider'=>$dataProvider,

    'columns'=>array(

        'username',

        'email',

        'status',

        array(

            'class'=>'CButtonColumn',

        ),

    ),

)); ?>

CGridView currently displays the ‘status’ number. How do I get it to display the statusText?

In the CDetailView I replaced ‘status’ with the following, but that didn’t work with CGridView:


array(

            'name'=>'status',

            'value'=>$model->getStatusText(),

        ),

Tried replacing $model with anything I could think of (e.g. $data, $data->model, $dataProvider, $dataProvider->data, $dataProvider->model, $this) but that eiter yields an empty result or a "Call to a member function on a non-object" errormessage. Please help!

recently I had a similar problem… I solved it this way:

in view:




        array(

            'name'=>'status',

            'value'=>'User::getStatusText($data->status)',

        ),



in User model:




public function getStatusText($status)

    {

        $msg=array(

            self::STATUS_UNCONFIRMED=>yii::t('core','Unconfirmed'),

            self::STATUS_ACTIVE=>yii::t('core','Active'),

            self::STATUS_DISABLED=>yii::t('core','Disabled'),

            self::STATUS_UNREGISTERED=>yii::t('core','Unregistered'),

        );


       return isset($msg[$status]) ? $msg[$status] : yii::t('core','Unknown');

    }



Thanks, that solved my issue!

I noticed this getStatusText() solution is the Yii 1.0 way and that the Yii 1.1 Blog demo version uses a Lookup table. This is quite interesting and I guess it’s a way forward. That solution in it’s currently documented shape however does not seem to support i18n yet. I guess this means if you want to work this way and support internationalization, you either have to extend the Lookup model with another model containing translations, or still wrap all output in yii::t() functions in the views. Given the fact that these are status fields which will (almost) never change, I prefer to stick with this for now.