Is it bad habit to work with multiple models in one controller action? Should it always be like one controller-one m开发者_JAVA技巧odel-one view?
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.
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.
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.
精彩评论