joe
joe

Reputation: 31

In the Yii framework how is the situation handled where 2 independant models are to be called in a single view?

I am a Yii newbie who is trying to understand how this particular situation would be handled.

If I have 2 models,

The first model contains date and other information pertaining to the model. The second model contains date and information pertaining to the second model.

I want to have a search where I enter the date - from there the information from both the first and second model are pulled and displayed for the given date.

What is the proper way to structure this in Yii?

Would I just add an action to one of the controllers? This seems somewhat arbitrary, as neither model is really connected to the other, besides the date.

Or do I just create a controller with no related model?

Your feedback is appreciated, as I am trying to understand the idea behind Yii.

I know there is a blog example, but in that case it would seem that the "post" model is the "main" element, and "comments" would be pulled and displayed for the relevant "post". But in my situation I'm curious how it is ideally structured, where the 2 models don't really have that kind of dependancy, but instead their only connection is the date.

Thank you.

Upvotes: 0

Views: 103

Answers (2)

sensorario
sensorario

Reputation: 21698

Yii is a MVC pattern. This means that a part of code stay on M (Model) and another in V (View) and another one in (C) Controller. You can find all your models in protected/models, all views in protected/views and so on.

A controller will render a view with model. Model is all the data you send to view. Model can be a variable, a record, an entire resultset. In controller you can make all query you need, and then you can put results into the view.

It's easy.

Or do I just create a controller with no related model?

Any controller has a related model. May you can be confused reading StuffController that is a controller and Stuff that is a model. This just means that Stuff model aims to work with stuff table. This also means that a controller has a lot of action that works with Stuff content.

All actions that works with post, can be putted in PostController. All action that works with a calendar, can be putted in a CalendarController. But Models and Controllers are logically separated.

So, ... an example

class DogController extends Controller {
    public function actionBark() {
        $this->render('theView', array(
            'varName' => 333
        ));
    }
}

This, in Yii's MVC, just means that: - Controller: is DogController - actionBark is an action that regards a Dog - Model: the model is '333' - View: the view is protected/views/dog/theView.php file and inside it the model is $varName that will be equals to 333. Model are data, and not necessary a record of a class that extends CActiveRecord or CModel.

Upvotes: 0

redGREENblue
redGREENblue

Reputation: 3126

First, properly define relations in your table/model.

Your controller code will change a bit to process both the models.

$model1= new ModelOne;
$model2= new ModelTwo;

Do the processing, loading model data/setting attributes etc..

Pass both the models instance to view.

$this->render('view',array(
    'model1'=>$model1,
    'model2'=>$model2,
    ));

In the view you can define fields/grid etc that renders data from both the models. You have to define/specify which associates with which fields.

Upvotes: 0

Related Questions