The difference between M method and D method in ThinkPHP

【Foreword】

      This article summarizes the difference between the M method and the D method in ThinkPHP. The following are personal opinions. Please correct me if there is any misunderstanding

 

【main body】

(1) Personal opinion:

       The parameters of the M() method are optional. If there is a parameter, it means that the parent class model is instantiated and the data table corresponding to the parameter is associated with the parameter; if no parameter is passed, it means that the parent class model is instantiated.

      The D() method is divided into two cases. When there are parameters, it means to instantiate the custom model, and when there are no parameters, it means to instantiate the parent model.

      Note: For general CURD operations, use the M method to instantiate the parent class model. Unless the needs of the parent class model cannot meet the development, you need to customize the model. For example, the attached form is divided into two types when submitting

     ①The attachment is saved and stored in $_FILES, and the specific process is written into the model;

     ②The saving operation of ordinary data can be completed in the controller

(2) Official Manual:

      What the two have in common is that they instantiate the model, but what are the differences between the two? Let's take a look together:

(1) D method

      $User = D('User'); The parameter User in parentheses corresponds to \Home\Model\UserModel.class.php of the corresponding model class file (we assume the current module is Home), if the parameter is 'UserType', then the corresponding \Home\Model\UserTypeModel.class.php of the model class file, that is to say, the parameter of the D method is the name of the model, and it is consistent with the case definition of the model class.

(2) M method

      $User = M('User'); is equivalent to $User = new \Think\Model('User');; that is, when the M method is instantiated, the system is directly instantiated by default \Think\Model class, if we want to instantiate other public model classes, we can use the following methods:

$User = M('\Home\CommenModel:User','think_','db_config');(We assume the table prefix is ​​think_)

(3) Summary

        In the process of instantiation, we often use the D method and the M method. The difference between the two methods is that the M method instantiates the model without requiring the user to define a model class for each data table. If the D method does not find the defined model class, then The M method is automatically called.

        In addition, if you want to use the automatic verification and automatic completion function in ThinkPHP, you need to use the D method.

        In fact, to put it bluntly, the parameter when the M method is instantiated is the table name of your database, and the D method instantiates the Model class you wrote in the Model folder. Of course, you want to automatically verify, automatically complete, and use the D method.

 

 

 

 

【share】

      Finally, share your opinions with others

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, and the M method does not read the model class, so automatic verification is invalid by default, but it can be achieved by dynamic assignment

 

And the D method must have to create the model class.

We can use the following two methods to create a mapping object of a data table

The first: $Test = D('Test')

The second: $Test = new Model('Test')

 

Although both of them can perform select, insert, delete, and udpate operations on data, there are big differences in data validation.

Using the first method to instantiate a model will have a data check function. If the title is not filled in, it will prompt "Please enter the title" (this is an automatic verification function provided by tp, of course, the verification needs to be defined in the corresponding model. condition);

If you use the second one, you don't have this data verification function, you need to verify manually.

Summarized as follows:

(1) The D function instantiates the module under the Lib/Model of your current project. If the module does not exist, directly return the object that instantiates the Model (the meaning is the same as the M() function).

(2) M only returns the object that instantiates the Model. Its $name parameter is used as the table name of the database to handle operations on the database.

 

(1) In layman's terms:

D is to instantiate a Model based on the Model file.

M is to dynamically instantiate a Model object by directly instantiating the Model method (ThinkPHP base class), even if the corresponding Model file does not exist.

(2) To put it more simply:

The M instantiation parameter is the table name of the database.

D instantiates the model file you created under the Model folder.

D就是在你没有定义模型的时候,系统自动帮你定义一个模型,这样你才进行简单的数据输入或者输出。

每一个Action文件都应该对应Model文件的,如果你定义了Model的话,

如:$Form = D(“User”)就可以改成$Form = new  UserModel();(User是指你的模型文件名)。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326082364&siteId=291194637