Optional Url Parameter And Effect On The View File

I have a controller which has an optional id as input parameter. The id represents a primary key of a book in a table called tbl_book.

In my view file I have a search form where a user can search for one or more books. The user can as default enter the page without a given book id in the URL, thus the report view is rendered without any model loaded.

Since there is no model loaded then, I am not able to use the following in my view file.

What should I replace these three lines in the view file with, when there is no model loaded? Can you please point me in the right direction?

One way I did not go further is to do like this in the view file:

Is that the correct way to do it?

How about this


public function actionReport($id = null) 


    $model = $id ? $this->loadModel($id) : null;


    $this->render('report', array(

        'model' => $model,

        'id' => $id

    )); # btw you can use compact('id', 'model') instead of array(...)



<? if ($model): ?>

normal form

<? else: ?>

another form

<? endif ?>

Great, thank you ORey!