The specific differences between M and D methods in thinkphp
The difference between the M method and the D method
Both the M method and the D method in thinkphp are used to instantiate a model class, and the M method is used to instantiate an underlying model class efficiently, whereas the D method is used to instantiate a user-defined model class.
Using the M method
If this is the case, consider using the M method:
- When a simple curd operation is performed on a data table without complex business logic
- The M method is used in conjunction with the instantiated Commonmodel class when only the individual tables have more complex business logic
The M method can even simply look at the operation of the data table corresponding to the parameter table name:
Using the D method
If this is the case, consider using the D method:
- You need to use some of the advanced features in the thinkphp model, such as the auto-Validate function (implemented in the Create () method), the correlation model, etc.
- Business logic is more complex and involves many tables
- The business logic is defined in the Custom model class (under the Lib/model directory), and you want to implement these business logic in the operation
In addition, the D method does not support cross-project calls, and requires the use of:
$User = D (' User ', ' Admin '); Instantiate the User model under the Admin project $User = D (' Admin.user ');
Tips
When item grouping is enabled, the Model class does not necessarily correspond to the grouping of items. A model class that is common among multiple project groups and is placed uniformly in the model directory and can be instantiated directly using D (' modelname '). D (' user.userinfo ') does not mean that User must be a project group, or just a catalog of files under model, and D (' user.userinfo ') instantiates the UserInfo model class under the User directory.
Summarize
Both M and D methods can be used directly in case the model class file does not exist, but the M method is obviously more efficient, but to use the business logic inside the model class, the D method must be used.
A comparative image of the analogy is: M method just like a computer installed in the operating system, can only use some basic functions, and the D method is installed on the system, such as Office, QQ and other application software, more powerful, while the whole computer running speed also slowed.
Above is a summary of the difference between M method and D method, and the M method and D method should be selected according to the actual situation.