First of all introduce thinkphp function: M method
The M method is used to instantiate an underlying model class, and the D method differs from the following:
1, does not need the custom model class, reduces the IO loading, the performance is good;
2, after the instantiation can only invoke the Basic model class (default is model Class) in the method;
3, you can specify the table prefix, database and database connection information at the time of instantiation;
The strength of the D method is reflected in the number of custom model classes you encapsulate, but as the new thinkphp framework's basic model classes become more powerful, the m approach is more practical than the D approach.
Call format for the M method:
M (' [Base model name:] Model name ', ' data table prefix ', ' database connection information ')
Let's take a look at the specific usage of the M method:
1, the instantiation of the basic model (models) class
In the absence of any model definition, we can instantiate a model class using the following method:
Instantiate the user model
$User = M (' user ');
Perform other data operations
$User->select ();
This method is the simplest and most efficient because it does not need to define any model classes, so it supports cross project calls. The disadvantage is that there is no custom model class, so the associated business logic cannot be written, and only basic curd operations can be completed.
is actually equivalent to:
$User = new Model (' User ');
Represents an Action Think_user table. The M method and the D method also have singleton functions, and multiple invocations are not repeated instantiations. The M method's model name parameter is automatically converted to lowercase when converted to a datasheet, which means that the thinkphp data table naming specification is in full lowercase format.
2. Instantiate other common model classes
The first way to instantiate because there is no definition of a model class, it is difficult to encapsulate some additional logical methods, but in most cases, perhaps just to extend some common logic, you can try one of the following methods.
$User = M (' Commonmodel:user ');
The change in usage is actually equivalent to:
$User = new Commonmodel (' User ');
Because the system's model classes can be loaded automatically, we do not need to manually perform a class library import before instantiating it. Model class Commonmodel must inherit models. We can define some common logical methods within the Commonmodel class, eliminating the definition of specific model classes for each datasheet, if your project already has more than 100 data tables, and most of the cases are basic curd operations, Only the individual models have some complex business logic that needs to be encapsulated, so the combination of the first and the second is a good choice.
3. Incoming table prefixes, databases, and other information
The M method has three parameters, the first parameter is the model name (which can include the underlying model class and the database). The second parameter sets the prefix for the data table (left blank to take the table prefix of the current project configuration), and the third parameter is used to set the database connection information that is currently in use (leave the database connection information for the current project configuration blank), For example:
$User = M (' DB2. User ', ' think_ ');
Represents the instantiation model class and operates the Think_user table in the DB2 database.
If the second argument is left blank or not, it means that the data table prefix in the current project configuration is used, and if the operation's datasheet does not have a table prefix, you can use:
$User = M (' db1. User ', null);
Represents the instantiation model class and operates the user table in the DB1 database.
If you are working in a database that requires a different user account, you can pass in the connection information for the database, for example:
$User = M (' User ', ' think_ ', ' mysql://user_a:1234@localhost:3306/thinkphp ');
The basic model class is modeled, then the Think_user table is operated, the database is connected with the User_a account, and the operation database is thinkphp.
The third connection information parameter can be used with DSN configuration or array configuration, or even configuration parameters can be supported.
For example, in a project configuration file, you configure:
' Db_config ' => ' mysql://user_a:1234@localhost:3306/thinkphp ';
You can use the following:
$User = M (' User ', ' think_ ', ' db_config ');
The underlying model classes and databases can be used together, for example:
$User = M (' Commonmodel:db2. User ', ' think_ ');
If you want to instantiate a layered model, using the common model class, we can use:
To instantiate the Userlogic, although this does not make sense because you can use the
Achieve the same function.
thinkphp Function: R method
The R method is used to invoke the operation method of a controller, and is further enhanced and supplemented by a method. See here for the usage of a method.
Call format for the R method:
R (' [item://][Grouping/] Module/operation ', ' parameter ', ' Controller layer name ')
For example, we define an action method that is:
Class Useraction extends Action {public
function detail ($id) {return
M (' User ')->find ($id);
}
Then you can call this action method in other controllers by using the R method (the general R method is used to call across modules)
$data = R (' User/detail ', Array (' 5 '));
Represents the detail method that invokes the user controller (the detail method must be a public type), and the return value is a user data with Query ID 5. If you want to invoke an action method that has no arguments, the second argument can be left blank and used directly:
$data = R (' User/detail ');
You can also support cross grouping and project calls, such as:
R (' Admin/user/detail ', Array (' 5 '));
Represents the detail method that invokes the user controller under the Admin group.
R (' Admin://user/detail ', Array (' 5 '));
Represents the detail method that invokes the user controller under the Admin project.
The official advice is not to make too many calls on the same floor, which can cause logical confusion, the part of the public call should be encapsulated into a separate interface that can be used to add a single controller layer to the interface call by using 3.1 of the new feature multilayer controllers, for example, we add an API controller layer,
Class Userapi extends Action {public
function detail ($id) {return
M (' User ')->find ($id);
}
Then, use the R method to call the
$data = R (' User/detail ', Array (' 5 '), ' Api ');
In other words, the third parameter of the R method supports the controller layer that specifies the call.
Also, the R method invokes the action method to support the operation suffix setting C (' Action_suffix '), and if you set the action method suffix, you still do not need to change the way the R method is invoked.
The above content to share the thinkphp function of the M method and the R method, I hope to help.