ThinkPHP specific difference M method and D method skills

Source: Internet
Author: User
Both the M and D methods in ThinkPHP are used to instantiate a model class. The M method is used to efficiently instantiate a basic model class, and the D method is used to instantiate a user-defined model class. if the M method is used, consider using the M method for the data table.

Both the M and D methods in ThinkPHP are used to instantiate a model class. The M method is used to efficiently instantiate a basic model class, and the D method is used to instantiate a user-defined model class.

Use M method

If the following is the case, consider using the M method:

When performing simple CURD operations on a data table without complex business logic

Only when some tables have complicated business logic, the M method and the instantiated CommonModel class are used in combination.

The M method can even be simply used to operate the data table corresponding to the parameter table name:

$ User = M ('user ');

Use method D

If the following is the case, consider using the D method:

You need to use some advanced features in the ThinkPHP model, such as the auto-verification feature (implemented in the create () method) and associated model.

The business logic is complex and involves a large number of tables

Define the business logic in the custom Model class (under the Lib/Model Directory), and want to implement these business logic in the operation.

In addition, the method D does not support cross-project calls. you need to use the following:

  1. $ User = D ('user', 'admin'); // instantiate the User model under the Admin project $ User = D ('admin. user'); // The Project Group is enabled.

Tip:After a project group is enabled, the Model class does not necessarily correspond to the project group. The Model classes shared by multiple project groups are stored in the Model Directory. you can directly use D ('modelname ') instantiate, while D ('user. userInfo ') does not mean that the User must be a project group, or a classification directory of the files under the Model, D ('user. userInfo ') instantiate the UserInfo model class under the User directory.

Summary:Both the M method and D method can be used directly when the model class file does not exist, but obviously the M method is more efficient, but the business logic in the model class should be used, you must use the D method.

The M method is like a computer with an operating system installed. it can only use some basic functions, the D method is like installing some application software such as Office and QQ on the installed system, which is more powerful and the whole computer runs slowly.

The above is a summary of the differences between M and D methods. M and D methods should be selected based on actual conditions.

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.