The difference between
D() and M() methods: The main difference between
D and M is that the
M method does not need to create a model class file. The M method does not read the model class, so automatic verification is invalid by default. , but it can be achieved through dynamic assignment
And the D method must create a model class.
We can use the following two methods to create a mapping object of a data table
The first one: $Test = D('Test')
The second one: $Test = new Model('Test')
Although both can perform select, insert, delete, and udpate operations on data, there is a big difference in
data verification.
Instituting a model in the first way will have a data checking function. If the title does not have When filling in, it will prompt "Please enter the title" (this is an automatic verification function provided by tp, of course, the verification conditions need to be defined in the corresponding model);
If you use the second type, you will not have this data verification function. Manual verification is required.
The summary is as follows:
The D function instantiates the module under Lib/Model of your current project.
If the module does not exist, directly return the object of the instantiated Model (the meaning is the same as the M() function).
And M only returns the object of the instantiated Model. Its $name parameter is used as the table name of the database to handle operations on the database.
In layman’s terms:
D is to instantiate a Model based on a 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.
To put it more simply:
M instantiation parameter is the table name of the database.
D is instantiated by the model file you created under the Model folder.
D means that when you do not define a model, the system automatically defines a model for you, so that you can perform simple data input or output.
Every Action file should correspond to a Model file. If you define a Model,
For example: $Form = D("User") can be changed to $Form = new UserModel(); (User refers to your model file name).