Getter function in cgridview column value


I have a hard time finding out why this doesn’t work or how it should. In CGridView, I have a column where I want to modify the value with a getter function from a model I have loaded. But this doesn’t work:

		array(	'name' => 'answer',

				'type' => 'raw',

 				'value' => '$model->getAnswerText("$data->answer")',


This gives me


Fatal error: Call to a member function getAnswerText() on a non-object in C:\xampp\htdocs\yii-1.1.10\framework\base\CComponent.php(607) : eval()'d code on line 1

This, of course does not work, either:

		array(	'name' => 'answer',

				'type' => 'raw',

 				'value' => $model->getAnswerText('$data->answer'),


$data->answer is a a string with the value $data->asnwer. Without quotation marks, it is null.

It seems like the first case should work, but somehow the evaluation fails. But $model is an object! Does it stop being an object, when the widget is run? This is really odd.

i got similar confusion while ago when i started with yii,

here is what i found in gridview:

if you use $data, always single-quote it up, like


if you use $model, don’t use single quote around it, so try this

array(  'name' => 'answer',

	'type' => 'raw',

	'value' => $model->getAnswerText($model->answer),


if you can change your code as this example does, then your problem will dissappear:

first, in your model:

public function getMyAnswerText() {

   return $this->getAnswerText($this->answer);


// becusae this becomes as a it was a normal "field" in  your model, so you can call as $model->myanswertext, then you should put a label too for it:

public function attributeLabels(){

    return array('myanswertext'=>'This is my answer');


then in your CGridView, you simply call this field in a regular way:

array('name' => 'myanswertext'),


thanks for the ideas, but they do not really encapsulate what I need to do:

@rootbear: The problem is that I need $data->answer as an argument for the function in the model. ($model is not the dataProvider). And exactly this works neither with quotation marks nor without.

@bluyell: the problem is that I need to pass a value from $data to the function. This is apparently not possible together with a model function

What I could do is put the data I need into the dataprovider. But I wanted to prevent this, since I thought the overhead would be too much.

$data->answer is an id that is related to another model, and I wanted to get the text of that model. But I guess it’ll work as well if I create a relation and include the data in the data provider.

Hi Narretz.

You can try this. I found my answer here.

CGridView: Render customized/complex datacolumns

Hi Narretz,

Well, ‘value’ property of a data column should be an expression string, which is later evaluated for each data of rows. That’s why you have to quote it with quotation marks(it doesn’t matter single or double).

Assuming the following code:

$model = new Foo;

$dataProvider = new CActiveDataProvider('Bar', ...);

$this->widget('CGridView', array('dataProvider'=>$dataProvider, ...));

You can not include $model in ‘value’, because it is out of scope when the expression is evaluated.

The objects that we can access in the expression are $data, $row and $this.

In the expression $data is an instance of Bar in a specific row.

So you can call Bar::someMethod like this.

    'value' => '$data->someMedhod()',

You can also call Foo::someMethod if it is a static method

    'value' => "Foo::SomeMethod($data->answer)",

But, if the relation between 2 models is a simple BELONGS_TO relation, I guess it is from your last post, then you should consider making use of the relation. It’s usually far much simple and effective.

    'value' => '$data->foo->answerText',

I am not sure if you are still searching for the solution,

Here is how I do it.

If you have a mehtod in your model named :

public function somemethod($someArg){



return $someVal;


then you can call this at a column of a Cgridview as

array(  'name' => 'answer',

                                'type' => 'raw',

                                'value' => '$data->somemethod($data->answer)',


This works just fine.

But the next step, how do you make this column a filterable ???

Can anyone help me ???

I am tired of googling about this :(

view Cgridview










in model function subjectFormated()

public static function subjectFormated($model){

       ////will get model->subject 

.....your code...................