Detailed M method and r method of TP3 function

Source: Internet
Author: User
This paper introduces the M method and the R method of the ThinkPHP3 function, the M method is used to instantiate a basic model class, and the R method is used to invoke the operation method of a controller, and the Friends of interest follow the script's small compilation together to learn it. I hope we can help you.

First, we introduce the thinkphp function in detail: M method

The M method is used to instantiate a base model class, and the difference between the D method is:

1, do not need to customize the model class, reduce IO loading, good performance;

2, after instantiation can only invoke the Basic model class (the default is the Model Class) in the method;

3, you can specify the table prefix, database and database connection information at the time of instantiation;

The power of the D method is reflected in how strong the custom model classes you encapsulate, but as the new thinkphp framework's underlying model classes become more powerful, the M method is more practical than the D method.

The invocation format of the M method:

M (' [Base model name:] Model name ', ' data table prefix ', ' database connection information ')

Let's look at how the M method is used specifically:

1. Instantiation of the Basic model class

When no model is defined, we can instantiate a model class using the following method:

Instantiate the User model $user = M (' User ');//Perform other data operations $user->select ();

This approach is the simplest and most efficient because there is no need to define any model classes, so cross-project calls are supported. The disadvantage is also that because there is no custom model class, the related business logic cannot be written and only basic curd operations can be done.

$User = M (' User ');

is actually equivalent to:

$User = new Model (' User ');

Represents the Action Think_user table. The M method has a singleton function as well as the D method, and multiple invocations do not repeat the instantiation. The model name parameter of the M method is automatically converted to lowercase when converted to a data table, which means that the thinkphp table naming specification is in all lowercase format.

2. Instantiation of other public model classes

The first method is instantiated because there is no definition of the model class, so it is difficult to encapsulate some additional logical methods, but in most cases, perhaps just need to extend some common logic, then you can try one of the following methods.

$User = M (' Commonmodel:user ');

The change usage is actually equivalent to:

$User = new Commonmodel (' User ');

Because the model classes of the system can be loaded automatically, we do not need to manually perform class library import operations prior to instantiation. Model class Commonmodel must inherit models. We can define some common logic methods in the Commonmodel class, and we can omit to define a specific model class for each data table, if your project already has more than 100 data tables, and most of the cases are some basic curd operations, Just the individual models have some complex business logic that needs to be encapsulated, so the combination of the first and second approaches 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 database), the second parameter sets the data table prefix (leaving the table prefix for the current project configuration blank), and the third parameter sets the database connection information currently in use (leaving the database connection information for the current project configuration blank). For example:

$User = M (' DB2. User ', ' think_ ');

Represents the instantiation of the model class and the operation of the Think_user table in the DB2 database.

If the second argument is left blank or does not pass, the data table prefix in the current project configuration is used, and if the data table for the operation does not have a table prefix, then you can use:

$User = M (' db1. User ', null);

Represents the instantiation of the model class and the operation of the user table in the DB1 database.

If you are working on a database that requires a different user account, you can pass in the connection information of the database, for example:

$User = M (' User ', ' think_ ', ' mysql://user_a:1234@localhost:3306/thinkphp ');

The basic model class is represented by the models, then the Think_user table is manipulated, the database is connected with the User_a account, and the operation database is thinkphp.

The third connection information parameter can use a DSN configuration or an array configuration, and can even support configuration parameters.

For example, in the project configuration file, configure the following:

' Db_config ' = ' mysql://user_a:1234@localhost:3306/thinkphp ';

You can use:

$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 are instantiating a hierarchical model, using the public model class, we can use:

M (' Userlogic:user ');

To instantiate the Userlogic, although this does not make much sense because it can be used

D (' User ', ' Logic ');

Achieve the same functionality.

thinkphp Function: R method

The method of operation of the R method for invoking a controller is further enhanced and supplemented by the a approach. See here for the usage of a method.

The call format for the R method:

R (' [Project://][Group/] Module/operation ', ' parameters ', ' Controller layer name ')

For example, we define an action method that:

Class Useraction extends Action {public Function detail ($id) {  return M (' User ')->find ($id);}}

Then you can call this operation method in the other controller through the R method (the general R method is used for cross-module invocation)

$data = R (' User/detail ', Array (' 5 '));

Represents the detail method that invokes the user controller (the detail method must be of the public type), and the return value is a user data with a query ID of 5. If the action method you want to invoke is not a parameter, the second argument can be left blank and used directly:

$data = R (' User/detail ');

You can also support cross-grouping and project invocation, for example:

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 recommendation is not to have too many calls on the same layer, which can cause logical confusion, the part that is called by the public should be encapsulated as a separate interface, with a new 3.1-feature multilayer controller that adds a single controller layer for interface calls, for example, we add an API controller layer,

Class Userapi extends Action {public Function detail ($id) {  return M (' User ')->find ($id);}}

Then, using the R method call

$data = R (' User/detail ', Array (' 5 '), ' Api ');

That is, the third parameter of the R method supports the controller layer that specifies the call.

At the same time, the R method calls the action method can support the operation suffix set C (' Action_suffix '), if you set the action method suffix, still do not need to change the method of call R methods.

Related recommendations:

The realization of ThinkPHP5 instance

Thinkphp analysis and resolution of the form token error

Detailed thinkphp how to implement generate and verify verification code

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.