Yii Framework Forum: Displaying model function output in a CGridView - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Displaying model function output in a CGridView Rate Topic: -----

#1 User is offline   toMeloos 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 19-September 09

Posted 11 February 2010 - 03:01 AM

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!
0

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,353
  • Joined: 12-October 09
  • Location:Croatia

Posted 11 February 2010 - 04:02 AM

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');
    }

Find more about me.... btw. Do you know your WAN IP?
0

#3 User is offline   toMeloos 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 19-September 09

Posted 11 February 2010 - 06:02 AM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users