Questions about the role of model in the MVC pattern

Source: Internet
Author: User
The M (model) in MVC represents models, which are mainly used to manipulate data, so the simple processing of the data should be on the C layer or the M layer.

$sql = "SELECT id, name FROM test_table"$result = $conn->query($sql);$data = $result->getAll();

In the above code, if I need to change the data to an array with the ID key, then this operation should be in the M layer or the C layer?

Reply content:

The M (model) in MVC represents models, which are mainly used to manipulate data, so the simple processing of the data should be on the C layer or the M layer.

$sql = "SELECT id, name FROM test_table"$result = $conn->query($sql);$data = $result->getAll();

In the above code, if I need to change the data to an array with the ID key, then this operation should be in the M layer or the C layer?

Personal opinion: M-layer comparison okay
Now that you're using MVC, you're trying to reduce the coupling level and improve scalability.
Mixed written words are not conducive to the above two purposes.
Of course, if the project is not small, there is no need to use the MVC structure.

Depending on the problem in your example, I think you can do it using closure function, such as defining a DataFormat method in the model class, and then invoking the return data. This dataformat again uses the closure function of your incoming class to process your return data, which is more flexible

In fact, all the line, specifically to see your interface requirements, not too tangled! Because of this problem I also asked my teacher, I said that the C layer is responsible for the control page jump and encapsulation request data interface, and M layer is the C layer call interface parameter check and data processing return, that is what business logic put in, in fact, this is our own design program needs to do

So don't be too tangled.

The model is in order to control all the data layers in the model, why do you do this?

耦合性降低了、可读性、维护、扩展性都得到了提高。

The C of MVC is to connect the model and view

The core idea of MVC is the Separation Interface (View), logic (Controller), and data (Model).
For Web applications, the process is C-call M output v.

Page Controller:

/post.php?a=create/post.php?a=read&id=1024/post.php?a=update&id=1024/post.php?a=delete&id=1024

Model (CRUD):

function post_create() {}function post_read() {}function post_update() {}function post_delete() {}

View:
/view/post.php

The crud operation function that is visible to the data model post is, of course, aggregated in a /include/funclass.php good way, and can be easily invoked in other controllers. In short, you think that the other controller also needs to be used, then you can consider encapsulating into a function or class put into the funclass.php "sharing."

Must be operating inside the controller.

Bold text请输入代码

  • 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.