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