久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 MVC 中,我在哪里放置數據庫查詢?

Where do I put a database query in MVC?(在 MVC 中,我在哪里放置數據庫查詢?)
本文介紹了在 MVC 中,我在哪里放置數據庫查詢?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

最近幾天,我大量閱讀了關于 PHP 中的 OOP 和 MVC 的書籍和網頁,以便我可以成為一個更好的程序員.我對 MVC 的理解遇到了一個小問題:

我應該把 mysql_query 放在哪里?

我應該將它放在控制器中并調用模型上的方法,該方法根據提供的查詢返回數據嗎?還是我應該把它放在模型本身?我提供的兩個選項都是垃圾嗎?

解決方案

MVC 主題的材料

您可以列出您正在閱讀的書籍,因為大多數(如果不是全部)涉及 MVC 的 php 書籍都是錯誤的.

如果您想成為一名更好的開發人員,我建議您從 Martining Fowler 的文章開始 - 文章.

控制器做什么?

Controller 在 MVC 中有一項主要職責(我將在這里討論 Model2 實現):

<塊引用>

對來自模型層(服務或領域對象)的結構執行命令,這會改變所述結構的狀態.

它通常具有次要職責:將結構從模型層綁定(或以其他方式傳遞)到視圖,但如果您遵循 SRP

SQL 相關代碼放在哪里?

信息的存儲和檢索在數據源層處理,通常實現為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);

如您所見,域對象在任何時候都不知道,來自它的信息已被存儲.也不是關于你把數據放在哪里.它可以存儲在 MySQL 或 PostgreSQL 或一些 noSQL 數據庫中.或者可能推送到遠程 REST API.或者也許映射器是測試的模擬.你需要做的就是替換映射器,為這個方法提供不同的工廠.

另外,請查看以下相關帖子:

  • 了解 PHP 中的 MVC 視圖
  • 具有依賴項的可測試控制器
  • 服務之間應該如何通信?
  • 面向高級 PHP 開發人員的 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 中,我在哪里放置數據庫查詢?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Action View Helper in Zend - Work around?(Zend 中的動作視圖助手 - 解決方法?)
Is this a good way to match URI to class/method in PHP for MVC(這是將 URI 與 PHP 中用于 MVC 的類/方法匹配的好方法嗎)
Where do I save partial (views) in Zend Framework, to be accessible for all Views in my App?(我在哪里保存 Zend Framework 中的部分(視圖),以便我的應用程序中的所有視圖都可以訪問?) - IT屋-程序員軟件開發技術
Having a single entry point to a website. Bad? Good? Non-issue?(有一個網站的單一入口點.壞的?好的?沒問題?)
Is MVC + Service Layer common in zend or PHP?(MVC + 服務層在 Zend 或 PHP 中常見嗎?)
Hello World example in MVC approach to PHP(PHP MVC 方法中的 Hello World 示例)
主站蜘蛛池模板: 久久一二三区 | 久久久久久久久国产成人免费 | 国产一区精品在线 | 日本小电影在线 | 欧美日韩福利 | 九九国产在线观看 | 999精品在线观看 | 一区二区三区精品视频 | 久久久精品网 | 欧美日韩亚洲视频 | 成人特级毛片 | 欧美午夜精品 | 成人亚洲视频 | 91中文在线观看 | 久久伦理电影 | 99热热99| 亚州av| 亚洲高清在线 | 亚洲欧洲精品成人久久奇米网 | 亚洲va国产日韩欧美精品色婷婷 | 精品一二区 | 精品亚洲一区二区三区 | 亚洲一区二区三区免费 | 久操伊人 | 欧美1区 | 欧美日韩综合视频 | 中文字幕 国产 | 中文字幕在线观看成人 | 久久精品视频一区二区 | 野狼在线社区2017入口 | 欧美色999| 日本精品久久 | 中国一级毛片免费 | 国产精品久久久久久影视 | 亚洲欧美激情精品一区二区 | 一区二区成人 | 国产精品一区二区三区四区五区 | av资源在线看 | 欧美8一10sex性hd| 999久久久 | 欧美亚洲国产一区 |