問題描述
最近幾天,我大量閱讀了關于 PHP 中的 OOP 和 MVC 的書籍和網頁,以便我可以成為一個更好的程序員.我對 MVC 的理解遇到了一個小問題:
我應該把 mysql_query
放在哪里?
我應該將它放在控制器中并調用模型上的方法,該方法根據提供的查詢返回數(shù)據嗎?還是我應該把它放在模型本身?我提供的兩個選項都是垃圾嗎?
MVC 主題的材料
您可以列出您正在閱讀的書籍,因為大多數(shù)(如果不是全部)涉及 MVC 的 php 書籍都是錯誤的.
如果您想成為一名更好的開發(fā)人員,我建議您從 Martining Fowler 的文章開始 - 文章.
控制器做什么?
Controller 在 MVC 中有一項主要職責(我將在這里討論 Model2 實現(xiàn)):
<塊引用>對來自模型層(服務或領域對象)的結構執(zhí)行命令,這會改變所述結構的狀態(tài).
它通常具有次要職責:將結構從模型層綁定(或以其他方式傳遞)到視圖,但如果您遵循 SRP
SQL 相關代碼放在哪里?
信息的存儲和檢索在數(shù)據源層處理,通常實現(xiàn)為DataMapper(不要與濫用該名稱的 ORM 混淆).
以下是它的簡化用法:
$mapper = $this->mapperFactory->build(ModelMappersUser::class);$user = $this->entityFactory->build(ModelEntitiesUser::class);$user->setId(42);$mapper->fetch($user);如果 ($user->isBanned() && $user->hasBannExpired()){$user->setStatus(ModelMappersUser::STATUS_ACTIVE);}$mapper->store($user);
如您所見,域對象在任何時候都不知道,來自它的信息已被存儲.也不是關于你把數(shù)據放在哪里.它可以存儲在 MySQL 或 PostgreSQL 或一些 noSQL 數(shù)據庫中.或者可能推送到遠程 REST API.或者也許映射器是測試的模擬.你需要做的就是替換映射器,為這個方法提供不同的工廠.
另外,請查看以下相關帖子:
- 了解 PHP 中的 MVC 視圖
- 具有依賴項的可測試控制器
- 服務之間應該如何通信?
- 面向高級 PHP 開發(fā)人員的 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:
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?
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.
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 =]
Can I use MVC with PHP ?
Not really. At least not the classical MVC as it was defined for Smalltalk.
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.
What is 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).
????????????????????????????
The image is taken from Service Layer article on Fowler's site.
What does the Controllers do ?
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
Where do I put SQL related code ?
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).
Here is how a simplified use of it would look like:
$mapper = $this->mapperFactory->build(ModelMappersUser::class);
$user = $this->entityFactory->build(ModelEntitiesUser::class);
$user->setId(42);
$mapper->fetch($user);
if ($user->isBanned() && $user->hasBannExpired()){
$user->setStatus(ModelMappersUser::STATUS_ACTIVE);
}
$mapper->store($user);
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.
Also, please see these related posts:
- understanding MVC Views in PHP
- testable Controllers with dependencies
- how should services communicate between each other?
- MVC for advanced PHP developers
這篇關于在 MVC 中,我在哪里放置數(shù)據庫查詢?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!