Is it bad habit to work with multiple models in one controller action? Should it always be like one controller-one model-one view?
In MVC we cannot pass multiple models from a controller to the single view.
PHP frameworks typically follow the Model View Controller (MVC) design pattern. This concept separates the manipulation of data from its presentation.
Question: Can there be two+ models in 1 controller? Answer: yes. Create a wrapper model, put other two models in it.
In fact, in ASP.NET MVC, there are three distinct types of model: the domain model, view model and input model.
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..
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With