13

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

1
  • 1
    Excellent question! It's a pity that nobody seems to care about questions like that! Commented Oct 21, 2013 at 11:58

4 Answers 4

13

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..

Sign up to request clarification or add additional context in comments.

6 Comments

In my opinion the majority of your code should be in your models, of which there will be many classes. Definitely not a one-to-one mapping between models and controllers. That would be a bad thing.
Yep, everyone loves fat models :)
I concur with this answer, but I just want to point out that table != model.
Dont forget to push accept (click on the tick) if this helped out :)
First I did it like this that the controller only had one model so I had to instantiate other models inside the main model so that I could work with multiple models. It gained a bit hierarcihical. :P So it is possible to instantiate multiple models inside a controller action and they dont have to have anything related to each other..
|
4

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.

Comments

2

No, it's not a bad practice.

Controllers map to HTTP resources (aka. URIs) more than to your data models. A request for a certain resource (users, products, ...) may often require loading and/or storing data from/in various places (a DB table, the file system, a remote web service, ...), so a 1:1 controller:model mapping is probably even the exception rather than the rule. Unless your website is really simple.

Comments

1

Like everyone else is saying, you're free to do what you want. I'd suggest looking at the site for the framework you are using, and see how other people are using it. They often have "Projects using XXXX Framework" there.

And like troelskin's comment, 1 table does not always equal one model. Some of the examples using a basic Active Record pattern (like CodeIgniter) tend to go with the 1 table 1 model method.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.