控制器可以有数据库查询(MySQL)吗?如果是,什么时候? [英] Can a Controller have database queries (MySQL)? If yes, when?
问题描述
我正在阅读许多有关MVC的教程,所以我的问题是,一个完美的PHP MVC框架能否在Controller中包含数据库查询?据我了解,最舒适的方法是将所有数据库查询都放入Model中,对吗?如果我有POST或smth,我只需将该POST传递给Model即可进行所有插入等操作.
I am reading lots of tutorials on MVC, so my question, can a perfect PHP MVC framework have database queries in Controller? As I understand, the most comfortable way is to put all database queries in Model, right? And if I have POST or smth, I just pass that POST to Model and it makes all inserts and etc. ?
或者我缺少什么?如果Controller可以进行数据库查询,那会是什么情况?
Or I am missing something? And if Controller can have a database queries, in which situation would it be?
推荐答案
没有控制器可能没有任何与数据库相关的代码-任何数据库查询都可以存储在MVC体系结构的模型中-控制器仅适用于模型,而不能直接与数据库一起使用
No controller may not have any db related code - any DB queries may be stored in model in MVC architecture - controller only works with models, but not directly with DB
大多数框架都允许直接从Controller调用SQL-但是它不是MVC,而是一堆对象
Most frameworks will allow calling SQL directly from Controller - but then it is not MVC, but bunch of objects
这篇关于控制器可以有数据库查询(MySQL)吗?如果是,什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!