thinkphp the difference between the Instanced object M () and D (), the difference between select and find "Go"

Source: Internet
Author: User

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 instantiated such as $Demo = New Model (), then it is equivalent to $Demo = M ();


The difference between D and M is mainly:
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 can be achieved by dynamically assigning values
The D method must have a model class created.

We can use the following two ways to create a mapping object for a data table
First type: $Test = D (' Test ')
Second type: $Test = new Model (' Test ')
Although both of these can be select,insert,delete,udpate operations on the data,
Data validation is very different, in the first instance a model will have a data check function, if the title is not filled in the words will prompt "Please enter the title" (This is a TP provides an automatic verification function, of course, also need to define the corresponding model in the validation conditions);

If we use the second kind, there's no.

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

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

thinkphp2.0 version Test There is 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) "Ten"  ["Remark"] + string (4) "??"  ["createdate"] = + string (+) "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 (+) "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 also be used in the Find method, except that the Find method returns only one record, so 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!

Thinkphp the differences between the instantiated objects m () and D (), the difference between select and find "Go"

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.