What role does the Model play in mvc-php?

Source: Internet
Author: User
I have been programming in the MVC mode. I suddenly have some questions about the definition of the Model layer. I want to explain the other two layers, one is responsible for displaying the view, and the other is responsible for controlling the process, which is clear and clear, but what does the Model mean? Literally, it is called a model layer. What is a model? Most... I have been programming in the MVC mode. I suddenly have some questions about the definition of the Model layer. I want to explain the other two layers, one is responsible for displaying the view, and the other is responsible for controlling the process, which is clear and clear, but what does the Model mean?

Literally, it is called a model layer. What is a model? Most models are defined like this.

Class User extends Model {public function add (array $ user) {// add code} public function delete ($ id )...}

Is Model only an encapsulation of the interface for adding, deleting, querying, and modifying databases? Some people think that the Model should map data tables. Is it an ORM implementation?

Reply content:

I have been programming in the MVC mode. I suddenly have some questions about the definition of the Model layer. I want to explain the other two layers, one is responsible for displaying the view, and the other is responsible for controlling the process, which is clear and clear, but what does the Model mean?

Literally, it is called a model layer. What is a model? Most models are defined like this.

Class User extends Model {public function add (array $ user) {// add code} public function delete ($ id )...}

Is Model only an encapsulation of the interface for adding, deleting, querying, and modifying databases? Some people think that the Model should map data tables. Is it an ORM implementation?

The MVC concept comes from traditional desktop software development. In such an environment, when an event occurs, the Model can actively notify the View, this is not possible in the HTTP protocol (except for long-connection comet ). For a long time, the PHP industry's understanding and application of M and C in the MVC framework are not refined (of course, enough is enough to meet the needs of most businesses ). This leads to unclear MC hierarchy. PHPer does not have clear rules when writing code. In the end, whether the business logic is put in C or M, the common problems are:

  1. The C layer has too many responsibilities. For example, likes an answer to give the corresponding responder a reputation and write it into C.
  2. The M layer is too thin and inherits the Model (or DB class) of the Framework to add, query, modify, and delete databases.
  3. Non-database operations (such as calling Weibo OpenAPI) have to be packaged into Util classes
  4. User input ($ _ GET, $ _ POST) is run in disorder globally. Both the M layer and Util have

In most scenarios, PHP is used as a Web Application and Database Driven Application. Therefore, the rapid development frameworks of various Database Driven have also emerged. For example, the Model class of CakePHP simply defines several CURD operations for data tables. Qcodo automatically generates the MVC three-tier scaffolding code based on the data table structure.

I understand that the PHP application is a layer-5 structure, and the M layer should be split into Biz Model, DAO, and Infrastructure. paste the ppt I mentioned four years ago:



Full text download: "Evolution of Web Application ubuntures(ppt20032.16.ppt" http://vdisk.weibo.com/s/535FV

It depends on the scale and complexity of your project. If it is just a simple database CRUD, it is no problem that the Model is completely replaced by the ORM.

In my project, there are modules and the complexity of multiple data sources. The Model is subdivided into three layers:

The top layer is responsible for event scheduling and cache scheduling.

A layer is abstracted in the middle. I call it ModelItem. The data source of a ModelItem may be ORM or Webservice. The relationship between data and data can be bridge between modelitems, that is, the traditional relationship such as One2One and many2133. However, this relationship is not limited to ORM, but is generally applicable to all data, therefore, it is very likely that a data Product from the database can be linked to data from Taobao Webservice.

The bottom layer is the underlying Implementation of Data interfaces, including ORM and Webservice.

Project: https://github.com/AlloVince/eva-engi...

So my conclusion is: the Model function includes but is not limited to abstracting the database as an object. If the project is simple, the Model can be equivalent to the ORM. If it is complicated, the Model should be subdivided.

An application includes application logic and business logic. (Reference link: http://www.wo2jia.cn/home/index.php? C ...)

The application logic is mainly process control. For example, you need to check whether there is space in the shopping cart when logging on to an account or adding items to the shopping cart before operating the account. These are stored in the Controller. The business logic is the data processing logic, such as adding products to the shopping cart and removing products from the shopping cart. To separate it from process control, we encapsulate the business logic and call it a Model ).

So what is "model "? According to the encyclopedia

Based on the specific purpose of the study, people reproduce the material form or thinking form of the structure, function, attribute, relation, process, and other essential features of the prototype object under certain assumptions.

That is to say, the model can be usedDescriptionA prototype object, such as a proportional model, can "describe" the actual ship structure and features.

The business model can be understood in this way. In reality, we exchange and process information, and some structured data can be described in the program. For example, if you go to the supermarket to buy things, there will be a shopping cart, and then a commodity will be lost in it.AbstractionIsModelDescribe thisBusiness? First, you must have an abstract shopping cart, which can be a ShopCart class (OO) in the program, and then a Goods class that contains product information, then SC provides several methods to operate Goods. This process can also be calledModelingRight.

After modeling, how to store data is another thing. You can store databases or files. Strictly speaking, the model should have no idea how to store data, and it should be solved by using Proxy. As for ORM, it is also a model. You can read it carefully and think about it.

The key lies inAbstractionYou can understand the concept more and use it in your head.

Note: This is my own thinking process. Due to the limitations of the level, some descriptions may be inaccurate. If you have any questions, I can point out that we will discuss them together ,:)

Suppose: In addition to web page display, you also need to write CLI scripts for database data statistics. How do you design your Model to meet both the web page display and data statistics tasks?

Assume that you are using MySQL. One day you need to use PosgreSQL or even start using NoSQL. How do you design your Model to minimize the total amount of code modification?

After answering these two questions, I think your problem is solved.

If the architecture is based on the separation between the frontend and backend of popular APIs and restful APIs.

Php is basically provided as a data source. In mvc, php only processes the model and crud. Then php c is responsible for scheduling and processing the assembled Logic Data. v is no longer available.

Front-end js is also responsible for receiving data in the mvc front-end model and assembling the data displayed on the page.

The overall structure is changed to mcmvc.

With the development of the front-end, the framework for the event mechanism, such as the mvvm structure, is actually changed to mcmvvm

If the back-end is nosql, mmvvm c may not be needed either. Some logics are also placed on the front-end.

My understanding model can be shared and called by multiple controllers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.