我应该在MVC的哪里放置数据库查询? [英] Where do I put a database query in MVC?

查看:138
本文介绍了我应该在MVC的哪里放置数据库查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近几天,我广泛阅读了有关PHP中的OOP和MVC的书籍和网页,以便成为更好的程序员.我对MVC的理解遇到了一个小问题:

The last few days, I have extensively read books and web pages about OOP and MVC in PHP, so that I can become a better programmer. I've come upon a little problem in my understanding of MVC:

我在哪里放置mysql_query?

Where do I put a mysql_query?

我应该将其放在控制器中并在基于提供的查询返回数据的模型上调用方法吗?还是应该将其放入模型本身?我要提供全部垃圾的两个选项都是吗?

Should I put it in the controller and call a method on a model that returns data based on the provided query? Or should I put it in the model itself? Are both of the options I'm providing total garbage?

推荐答案

关于MVC的材料

您可能已经列出了正在阅读的书,因为接触MVC的大多数(如果不是全部)php书都是错误的.

Materials on the subject of MVC

You could have listed the books you were reading, because most (if not all) php books, which touch on MVC, are wrong.

如果您想成为更好的开发人员,我建议您从Marting Fowler的文章开始- GUI体系结构 .随后是同一位作者的书- 企业模式应用架构" .然后,下一步将是为您研究 SOLID原理,并了解如何编写遵循得墨meter耳法律的代码.这应该涵盖基础知识=]

If you want to become a better developer, i would recommend for you to start with article by Marting Fowler - GUI Architectures. Followed by book from same author - "Patterns of Enterprise Application Architecture". Then the next step would be for you to research SOLID principles and understand how to write code which follows Law of Demeter. This should cover the basics =]

并非如此.至少不是为Smalltalk定义的经典MVC

在PHP中,您有另外四个针对相同目标的模式:MVC Model2,MVP,MVVM和HMVC.再一次,我懒得再写一次关于差异的文章,所以我只链接到我的我的旧评论

Instead in PHP you have 4 other patterns which aim for the same goal: MVC Model2, MVP, MVVM and HMVC. Again, I am too lazy to write about differences one more time, so I'll just link to an old comment of mine.

您必须了解的第一件事是MVC中的Model不是类或对象.它是一个包含多个类的层.基本上,模型层是所有层的组合(尽管第二层应称为域对象层",因为它包含域模型对象").如果您希望快速阅读有关模型"层各部分内容的摘要,可以尝试阅读此旧注释(跳到旁注"部分.)

First thing you must understand is that Model in MVC is not a class or an object. It is a layer which contains multitude of classes. Basically model layer is all of the layers combined (though, the second layer there should be called "Domain Object Layer", because it contains "Domain Model Objects"). If you care to read quick summary on what is contained in each part of Model layer, you can try reading this old comment (skip to "side note" section).

             nbsp; b ;   
该图像取自Fowler网站上的服务层文章.

                            
The image is taken from Service Layer article on Fowler's site.

控制器在MVC中承担一项主要职责(我将在这里谈论Model2的实现):

Controller has one major responsibilities in MVC (I'm gonna talk about Model2 implementation here):

在模型层(服务或领域对象)的结构上执行命令,这些命令会更改所述结构的状态.

Execute commands on structures from model layer (services or domain objects), which change the state of said structures.

它通常具有第二责任:将结构从模型层绑定(或以其他方式传递)到视图,但是如果您遵循

It usually have a secondary responsibility: to bind (or otherwise pass) structures from Model layer to the View, but it becomes a questionable practice, if you follow SRP

信息的存储和检索在数据源层进行,通常以 DataMapper (请勿与滥用该名称的ORM混淆).

The storage and retrieval of information is handled at the Data Source Layer, and is usually implemented as DataMapper (do not confuse with ORMs, which abuse that name).

这是它的简化用法:

$mapper = $this->mapperFactory->build(Model\Mappers\User::class);
$user = $this->entityFactory->build(Model\Entities\User::class);

$user->setId(42);
$mapper->fetch($user);

if ($user->isBanned() && $user->hasBannExpired()){
    $user->setStatus(Model\Mappers\User::STATUS_ACTIVE);
}

$mapper->store($user);

如您所见,域对象甚至都没有意识到存储了来自该域对象的信息.而且,这两种情况都不会将数据放在何处.它可以存储在MySQL或PostgreSQL或某些noSQL数据库中.或者也许推送到远程REST API.也许映射器是一个模拟测试.要替换映射器,您所需要做的就是为其他工厂提供此方法.

As you see, at no point the Domain Object is even aware, that the information from it was stored. And neither it cases about where you put the data. It could be stored in MySQL or PostgreSQL or some noSQL database. Or maybe pushed to remote REST API. Or maybe the mapper was a mock for testing. All you would need to do, to replace the mapper, is provide this method with different factory.

  • understanding MVC Views in PHP
  • testable Controllers with dependencies
  • how should services communicate between each other?
  • MVC for advanced PHP developers

这篇关于我应该在MVC的哪里放置数据库查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆