passing variables from a view to another

The situation is this:

I have a doctor-client-visit db.

1 doctor -> N clients

1 client -> N visits

1 doctor -> N visits

1 client -> 1 doctor

From the list of clients I’d like to create a new visit for the client selected.

I started creating a new action in the admin.php of the view clients this way:




      <?php echo CHtml::link('New Visit',array('Visits/create','client_id'=>$model->id)); ?>




and then in the _form.php of the view "Visits" I did this:




<?php $cliente = Clients::model()->findAll(); ?>

<?php echo CHtml::activeLabelEx($model,'client_id'); ?>

<?php echo CHtml::dropDownList("client_id", $_GET['client_id'],CHtml::listData($client, 'id', 'nome')); ?>




it works, but I’m sure there’s a better way of doing this. Can anyone suggest where I could look to find it?

Don’t overcode your view. Use the controller instead.

At VisitController.php, do:




public function actionCreate(){

    $model = new Visit;

    ....

    if (isset($_GET['client_id'])){

        $model->client_id =$_GET['client_id'];

    }


    $clientList = CHtml::listData(Clients::model()->findAll() , 'id', 'nome');


    $this->render('create', array('model' => $model, 'clientList' => $clientList);  // don't forget to pass it to '_form.php' too.

}



On view:


<?php echo CHtml::activeDropDownList($model, "client_id", clientList); ?

Great! Thanks to you I started understanding all this MVC-Yii-OOP stuff :)

Now I moved a lot of code in the controller, and everything has become so simple (it IS really simple, indeed, but to me it seemed a chaos)

You know, sometimes a word opens someone’s eyes :)

thank you :)