Differences between shortcut keys M and D in ThinkPHP framework

Source: Internet
Author: User
The difference between D () and M () methods: the 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, therefore, automatic verification is invalid by default, but it can be implemented through dynamic assignment, and the D method must have a model class. We can use the following two methods to create a ing object for a data table: $ TestD ('test') and $ TestnewModel ('test') D () differences from M:

The main difference between D and M is that

The M method does not need to create a model class file, and the M method does not read the model class. by default, automatic verification is invalid, but it can be implemented through dynamic assignment.

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 ')

Both of them can perform select, insert, delete, and udpate operations on data.

There are big differences in data verification,

In the first method, the data check function is provided for a model instance. if the title is not entered, the system prompts "enter the title" (this is an automatic verification function provided by tp, of course, you also need to define the verification conditions in the corresponding model );

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


The D function instantiates the modules under the Lib/Model of your current project.
If this module does not exist, the object of the instantiated Model is directly returned (meaning it is the same as the M () function ).
M only returns the object that instantiates the Model. The $ name parameter is used as the table name of the database 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 define a 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.