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!