A
A
arab7892015-03-10 12:36:06
Yii
arab789, 2015-03-10 12:36:06

How to handle 0 in CGridView YII?

I make comments. If a comment is left by a guest, the user_id column remains "0" in the database, and an error occurs when building the table: "Trying to get property of non-object". How to handle null and indicate if null is a guest?

$this->widget('zii.widgets.grid.CGridView', array(
  'id'=>'comment-grid',
  'dataProvider'=>$model->search(),
  'filter'=>$model,
  'columns'=>array(
    'id' => array(
                    'name' => 'id',
                    'headerHtmlOptions' => array('width'=>30),
                ),
                'status' => array(
                    'name' => 'status',
                    'value' => '($data->status==1)?"Доступно":"Скрыто"',
                    'filter' => array(0=>"Скрыто",1=>"Доступно"),
                ),
    'content',
                'page_id' => array(
                    'name' => 'page_id',
                    'value' => '$data->page->title',
                    'filter' => Page::all(),
                ),
    'created' => array(
                    'name' => 'created',
                    'value' => 'date("j.m.Y H:i", $data->created)',
                    'filter' => false,
                ),
                'user_id' => array(
                    'name' => 'user_id',
                    'value' => '$data->user->usename',
                    'filter' => User::all(),
                ),
    'guest',
    array(
      'class'=>'CButtonColumn',
                        'updateButtonOptions' => array('style' => 'display:none'),
    ),
  ),
));

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vit, 2015-03-10
@arab789

'user_id' => array(
                    'name' => 'user_id',
                    'value' => function($data){return $data->user_id ? $data->user->username : 'guest';}
                    'filter' => User::all(),
                ),

M
Maxim Grechushnikov, 2015-03-10
@maxyc_webber

you don't have a user with id 0
'user_id' => array(
'name' => 'user_id',
'value' => '$data->user->usename',
'filter' => User::all() ,
),
here you need to check whether it is 0 or not

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question