The difference between the instantiation of the object M () and D () in thinkphp, the differences between select and find

Source: Internet
Author: User

Original: thinkphp The difference between the Instanced object M () and D (), the difference between select and find

The difference between instantiating objects m () and D () in 1.ThinkPHP

In the process of instantiation, the D method and M method are often used, the difference between the two methods is that the M method instantiation model does not require the user to define a model class for each data table, and the M method is automatically called if the D method does not find a defined model class.
Popular Point says:
The M instantiation parameter is the table name of the database.
D instantiates the model file you created underneath the Model folder.

For example: $user = new Usermodel ();equivalent to $user = D (' user ');If an empty model is instantiatedfor example $Demo = new Model ();then it is equivalent to $Demo = 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, so automatic validation is not valid by default, but it can be implemented dynamically by means of a dynamic assignment
and the D method must have a model class created.

We can use the following two methods to create a mapping object for a data table
First: $Test = D (' Test ')
Second: $Test = new Model (' test ')
Although both of these can select the data, Insert,delete,udpate operation, in the
data validation is very different, in the first instance of a model will have a data check function, if the title is not filled out, you will be prompted "Please enter the title" (This is the TP provides an automatic verification function, It is also necessary to define the validation conditions in the corresponding model);

If you use the second type, you will not have.

There are 1 other differences when using $truetablename, you must use $test=d (' test ') to indicate that the query is a test table,

If you are using $test=m (' Test '), That means that the data side of the query is think_test.

thinkphp2.0 version of the test has such a problem, D is to instantiate a model file-based model. M is dynamically instantiating a model object by directly instantiating the model method (thinkphp base class), even if the corresponding model file does not exist.

The following is a list of common alphabetic methods:
============================================================================
A quick instantiation of the Action class library
B Execution Behavior Class
C Configuration Parameter access method
D Fast instantiation of the Model class library
F Fast and simple text data access method
L Language Parameter access method
M fast High performance instantiation model
R quickly remotely invoke the Action class method
s Fast Cache access method
U URL dynamic Generation and redirection method
W Fast Widget Output method

The D function instantiates the module below the Lib/model of your current project.
If the module does not exist, return the object that instantiates the model directly (meaning is the same as the M () function).
and M only returns, instantiating the model object. Its $name parameter handles the operation of the database as a table name for the database.

The difference between select and find in 2.thinkphp

Thinkphp is a good PHP development framework, can be more rapid development of the MVC architecture Management System, we need to use the Select () and the Find () method, two methods can return the array of datasets, but what is the difference? Let's take a look at my code comparison:

$tech=m (' techlevel ', ' Hr_cs_ ', ' Db_config2 '$Data=$tech->where (' id=1 ')- >find (); Dump ($Data$Data=$tech->where (' id=1 '),Select (); Dump ($Data);

The results are as follows:

Array(6) {   ["ID"] = Int (1)   ["Techlevel"] = =string(2) "10"  ["Remark"] = =string(4) "??"  ["CreateDate"] = =string(19) "2013-03-14 15:14:38"  ["CreateBy"] = =string(5) "admin"  ["Row_number"] = =string(1) "1"}     Array(1) {   [0] = =Array(6) {     ["ID"] = Int (1)     ["Techlevel"] = =string(2) "10"    ["Remark"] = =string(4) "??"    ["CreateDate"] = =string(19) "2013-03-14 15:14:38"    ["CreateBy"] = =string(5) "admin"    ["Row_number"] = =string(1) "1"  } }

As can be seen from the above code, find () returns a one-dimensional array, select () returns a two-dimensional array, so the value is different,

One-dimensional array values are used $data ["Techlevel"],

Two-dimensional array values are used $data [0]["Techlevel"],

There is an important difference between the two: the operation of reading data is similar to a dataset, and all of the consistent operation methods available for select can be used in the Find method, except that the Find method returns only one record (no return data error). Therefore, the limit method is not valid for the Find query operation. Because at first did not understand this usage, debugging day is not worth, finally have the dump method to see the difference of two methods!

The difference between the instantiation of the object M () and D () in thinkphp, the differences between select and find

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.