Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP and MVC, multiple models

Is it bad habit to work with multiple models in one controller action? Should it always be like one controller-one model-one view?

like image 436
PPPHP Avatar asked Apr 26 '10 10:04

PPPHP


People also ask

Can MVC have multiple models?

In MVC we cannot pass multiple models from a controller to the single view.

Does PHP use MVC?

PHP frameworks typically follow the Model View Controller (MVC) design pattern. This concept separates the manipulation of data from its presentation.

Can a controller have multiple models?

Question: Can there be two+ models in 1 controller? Answer: yes. Create a wrapper model, put other two models in it.

How many models are there in MVC?

In fact, in ASP.NET MVC, there are three distinct types of model: the domain model, view model and input model.


2 Answers

No, of course it is not a bad habit. Where you work with multiple tables, then various models will be needed.

The problem is not so much with this, but with the frameworks ability to handle advanced queries, joins and filtering based on these models. It's one of those bugbears when you are "obliged" to follow a framework's particular aesthetic about database interaction, but this depends on, of course, which framework and how far you are relying on magic behaviours.

Not every project is a simple blog! :)

edit: I should say also, that this kind of thing is one of my main gripes with MVC frameworks in general. The compromise between what your project is attempting to achieve, and what the framework allows is always going to be where the hair is lost and the late nights invested..

like image 79
dmp Avatar answered Oct 24 '22 02:10

dmp


I couldn't imagine being able to only work with one model at a time. Those databases are called 'relational databases' for a reason. All the tables interact with each other.

I actually used a framework that only allowed loading of the one model that was associated with that controller. Definitely a horrible experience, the only operations that worked were basic CRUD actions.

like image 45
Lotus Notes Avatar answered Oct 24 '22 02:10

Lotus Notes