Mvc - Advice Needed

Hi,

Each time I create a record in my db, I need additional data obtained from a method in my controller. Which is the best way to do it and why? I actually do it in one of the two following ways:

I run the code in the actionCreate function itself and pass the data as a parameter like this




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


			'model'=>$model,

                        'data'=>$data,


		));



or I add a public function obtaindata() in the controller and call it in my create view like this:-




       $data = $this->obtaindata()



Thanks

I dont know exactly what you trying to achieve but I would use the first one

Thanks for your reply alirz23. Would you chose the first one because of the MVC concept or for another reason?

Here is what I need to do:

Extract the list of customers who are residing in a hotel on any particular day so that I may choose only from this list when I create orders for the restaurant of the hotel. So this is my code which I include in actionCreate:




     public function actionCreate()

	{

        $model=new Order1;

	$model->date= date('d-m-Y');		

		// building $data to display only current customers in view 

		list($d, $m, $y) = explode('-', date('d-m-Y'));

		$mk=mktime(0, 0, 0, $m, $d, $y);

		$date = date ('Y-m-d', $mk);

		$data =Reservation::model()->findAll('arrival<=:id && departure>=:id',array(':id'=> $date));	

.....

......

......

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

			'model'=>$model,

                        'data'=>$data,        

		));

}

What if I create a public function like this in my controller:




public function listclient()

	{	list($d, $m, $y) = explode('-', date('d-m-Y'));

		$mk=mktime(0, 0, 0, $m, $d, $y);

		$date = date ('Y-m-d', $mk);

		$data =Reservation::model()->findAll('arrival<=:id && departure>=:id',array(':id'=> $date));	

                return $data;

       }



And then in my create view, I call the method:





<?php $clients = $this->listclient(); ?>




you can do that too but the first is much more clear one more thing all your logic in the controller should be in the model

Instead of creating listClient function in controller, create similar in the model. Anything relevant to the database should be in Model. In controller, call this function of model, then pass it to the view. Don’t call it directly from view. This would be a better approach.

Thanks alirz & Ujjwal.

I’ll revise my code and keep that in mind for the future