MVC the method I used to call a function from another model is correct?

Asked

Viewed 67 times

0

I am studying MVC and came across the following situation:

I have a View that needs to receive data from two different Models, so in the controller of this View I have the function below:

public function teste($id) {

        $this->view->a = $this->getOneA($id);
        
        $this->loadModel('b');
        $this->view->b = $this->model->getOneB($id);

        $this->view->render('app/teste');
}

The above scenario returns me the expected, takes the data of A and B and makes them available to the View.

It is correct to use this method ?

If yes, I should load the Model "b" into the controller’s __Construct "a" or directly into the controller’s function "a" as in this example?

If not, what is the best way to get data from different models?

NOTE: All controllers are children of the main controller and all models are children of the main model.

1 answer

0


Is it correct to use this method ? If yes, I should load the Model "b" into the controller’s __Construct "a" or directly into the controller’s function "a" as in this example?

For those who go through the same doubt, the answer is yes to the above questions.

I can declare the model both in the Construct (enabling use anywhere in the class) and in a single function as in the example.

The structure is very similar (including method names) to Cakephp’s.

I found the answer here https://riptutorial.com/cakephp/example/11485/load-another-model-into-controller

Browser other questions tagged

You are not signed in. Login or sign up in order to post.