What role does the Model play in php?

Source: Internet
Author: User
The role played by the Model in php has been being programmed in the MVC mode. suddenly, I have some doubts about the definition of the Model layer. I would like to say that the other two layers are responsible for displaying a view, what does a Model refer to when controlling a process is clear and clear?

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 used to describe the prototype object. for example, a proportional model can "describe" the actual ship structure and characteristics.

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 a supermarket to buy things, you will have a shopping cart, and then a commodity will be lost in it. how can we abstract it into a model to describe this business? 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 called modeling.

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.

At the bottom, the key lies in abstraction, understanding more concepts, and understanding clearly in the 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 ,:)

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.