Home  >  Article  >  PHP Framework  >  The difference between thinkphp m and d

The difference between thinkphp m and d

步履不停
步履不停Original
2019-06-13 09:09:443332browse

The difference between thinkphp m and d

In the process of instantiation, D method and M method are often used. The difference between these two methods is that the M method instantiates the model without the user defining a model class for each data table. If the D method does not find the defined model class, the M method is automatically called. In layman's terms: the M instantiation parameter is the table name of the database. D instantiates the model file you created under the Model folder

例如:$user = new UserModel();
等价于$user = D('user');
如果实例化的是一个空模型
例如 $Demo = new Model();
那么它等价于 $Demo = M();

The main difference between D and M is : The M method does not require the creation of 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 the model class. We can use the following two methods to create a mapping object of a data table. The first is: $Test = D('Test') The second is: $Test = new Model('Test'). Although both of these can map data Performing select, insert, delete, and udpate operations is very different in data verification. Using the first method to instantiate a model will have a data checking function. If the title is not filled in, it will prompt "Please enter the title" (this It 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, it will not be available... There is another difference. When $trueTableName is used, it must Using $test=d('test') means that the query is the test table. If $test=m('test') is used, it means that the data side of the query is think_test. The thinkphp2.0 version test has the above problem

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

A quickly instantiates the Action class library B executes the behavior class C configuration Parameter access method D Fast instantiation of Model class library F Fast and simple text data access method L Language parameter access method M Fast high-performance instantiation model R Fast remote call Action class method S Fast cache access method U URL dynamic generation and Redirection method W and fast Widget output method D function instantiate the module under Lib/Model of your current project. If the module does not exist, the object of the instantiated Model is directly returned (the meaning is the same as the M() function). And 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.

For more ThinkPHP related technical articles, please visit the ThinkPHP usage tutorial column to learn!

The above is the detailed content of The difference between thinkphp m and d. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn