CGridView - error when object doesn't exist

I have a simple forum style discussion that you have to log in in order to post or read comments.

If you create a post or reply to a post the user id is stored with the post, so that I can identify and display the users name next to the post or comment.

It all works fine except when I delete the user, so now the post still exists but it points to a non existent user and consequently displays an error when I try to access the info in CGridView.

This is part of the view that displays the username in a column.


array(

                        'header'=>'Started By',    

			'name' => 'author',

			'type'=>'raw',

			'value' => '$data->author->username',

                        'htmlOptions'=>array(

                                            'width'=>'100px',

                                            ),

		),

‘author’ is a relation


'author' => array(self::BELONGS_TO, 'User', 'author_id')

Is there anyway that I can trap this error so that if the user has been deleted I can replace that with ‘author unknown’ or ‘user deleted’ or similar message.

The other approach would be to create a dummy user and modify all posts associated with the user when the user is deleted to now point to the dummy.

Any suggestions?

doodle

The value is "eval()" ed, so why not use an if statement?


array(

                        'header'=>'Started By',    

                        'name' => 'author',

                        'type'=>'raw',

                        'value' => 'isset($data->author) ? $data->author->username :"User deleted"',

                        'htmlOptions'=>array(

                                            'width'=>'100px',

                                            ),

                ),

Perfect!

Thank you Haensel.

doodle