CGridView and CDetailView column or attribute value by expression

CDetailView does not use “deferred” evaluation like CGridView does on rendering rows. CDetailView gives you direct access to your model, while CGridView only to a per-row prepared data. So, to render CGridView‘s column or CDetailView‘s attribute row, you must use slightly different approaches. Here is how.

To render CGridView column’s value based on evaluated expression, you have to use quotes and reffer to $data which contains your model’s data per particular row. For example:

'id'=>'users-grid',
'dataProvider'=>$model->search(),
'columns'=>array
(
    'id',
    'login',
    array
    (
        'name'=>'level',
        'value'=>'Yii::app()->user->getRole($data->level)'
    )
)

To achieve the same in CDetailView, you have to remove quotes (code will be directly executed, not evaluated) and rely directly on your model:

'data'=>$model,
'attributes'=>array
(
    'id',
    'login',
    array
    (
        'name'=>'level',
        'value'=>Yii::app()->user->getRole($model->level)
    ),
)

Some additional info you can find in these Yii Forum topics:

Note, however, that these are over two years old!

Leave a Reply