Confusion About Custom Function In Gridview

Sorry if this is a basic question, I am new to Yii and advanced PHP.

I am unsure how to call a custom function in GridView:

In my Controller


public function stars($num){

            return str_repeat("*", $num);

        }



And in my View


['class' => '\kartik\grid\DataColumn',

        'attribute' => 'domain_rating',

         'format' => 'raw',

          'value' => <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />,]



I don’t know how to send the data of domain_rating into the stars() function.

any help would be appreciated.

You do not need the function in your controller… you can simply use a Closure function like below in your case




[

   'class' => '\kartik\grid\DataColumn',

   'attribute' => 'domain_rating',

   'format' => 'raw',

   'value' => function ($model, $index, $widget) {

       return str_repeat("*", $model->domain_rating); 

   }

]



thank you for your reply.

but my confusion is where does $model, $index, $widget come from?

i keep getting an error ‘Trying to get property of non-object’

meaning I am not clear what the model name is.

From the DataColumn specs… each row in the gridview is a model instance which will be automatically passed (remember the GridView dataProvider contains a list of models).

thanks, that helped clear it up.

also my main problem was that


$model->domain_rating

didn’t work, i had to use


$model['domain_rating']

What might the reason be that i couldn’t access it via the


$model->domain_rating

way?

Not sure why really? Do you have similar names, relations, or getters as domain_rating. You may try also including domain_rating in the safe attributes list and retry.

maybe asArray() or ArrayDataProvider are using