Differences between the shortcut keys M and D in the ThinkPHP framework

Source: Internet
Author: User
The main difference between D and M is that M methods do not need to create model class files, and M methods do not read model classes, so automatic verification is invalid by default, however, the value can be dynamically assigned, and the D method must have a model class.

What is the difference between M and D in the ThinkPHP framework?

Differences between D () and M () methods:

The main difference between D and M is that M methods do not need to create model class files, and M methods do not read model classes, so automatic verification is invalid by default, however, the value can be dynamically assigned, and the D method must have a model class.

We can use the following two methods to create a data table ing object

First: $ Test = D ('test ')

Type 2: $ Test = new Model ('test ')

Although the two methods can perform select, insert, delete, and udpate operations on data, there is a big difference in data verification. in the first way, the data check function is provided when a model is instance, if the title is not entered, the system prompts "enter the title" (this is an automatic verification feature provided by tp, and the verification criteria must be defined in the corresponding model );

If this data verification function is not available in the second method, you need to manually verify it.

Summary:

The D function instantiates the modules under the Lib/Model of your current project. if this module does not exist, it directly returns the object of the instantiated Model (meaning it is the same as the M () function ). M only returns the object that instantiates the Model. Its $ name parameter serves as the database table name to process database operations.

In layman's terms:

D is to instantiate a Model based on the Model file.

M dynamically instantiates a Model object by directly instantiating the Model method (ThinkPHP base class), even if the corresponding Model file does not exist.

In other words:

The M instantiation parameter is the database table name.

D. instantiate the Model file created in the Model folder.

D. when you do not define a model, the system automatically defines a model for you to perform simple data input or output.

Each Action file should correspond to the Model file, if you have defined the Model.

For example, $ Form = D ("User") can be changed to $ Form = new UserModel (); (User indicates the name of your model file ).

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.