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