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

在 MVC 中,我在哪里放置數(shù)據庫查詢?

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

問題描述

最近幾天,我大量閱讀了關于 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模板網!

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

相關文檔推薦

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屋-程序員軟件開發(fā)技術
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 示例)
主站蜘蛛池模板: 日日夜夜天天 | 爱草在线 | 成人a在线 | 视频一二三区 | 婷婷综合在线 | 久草免费在线视频 | 精品国产乱码久久久久久88av | 欧美在线观看免费观看视频 | 久久精品毛片 | 国产免费一区二区三区免费视频 | 日韩在线中文字幕 | 欧美在线资源 | 一区二区精品视频 | 日韩1区| 国产黄色av网站 | 国产激情一区二区三区 | 精品国产1区2区3区 一区二区手机在线 | 亚洲精品国产偷自在线观看 | 国产精品色婷婷久久58 | av网站推荐| 婷婷国产一区 | 成人免费淫片aa视频免费 | 久久国产欧美日韩精品 | 天天干在线播放 | 成人精品一区二区三区 | 久久久久国产精品午夜一区 | 精品一区二区三区在线观看 | 欧美成人精品 | 9久9久9久女女女九九九一九 | 精品日韩在线观看 | 狠狠做六月爱婷婷综合aⅴ 国产精品视频网 | 国产一区二区在线免费观看 | 99热激情 | 国产午夜精品一区二区三区嫩草 | 国产精品亚洲一区 | 成人国产一区二区三区精品麻豆 | 欧美中文在线 | 国产激情视频 | 涩色视频在线观看 | 日韩精品免费一区 | 久久久久亚洲精品 |