This article introduces the M method and R method of ThinkPHP functions. The M method is used to instantiate a basic model class, and the R method is used to call the operation method of a controller, if you are interested in learning ThinkPHP with the help of the script house, first introduce the ThinkPHP function details: M method.
The M method is used to instantiate a basic model class. The difference between the M method and the D method is:
1. You do not need to customize model classes to reduce I/O loading and improve performance;
2. After instantiation, you can only call methods in the basic Model class (default: Model class;
3. You can specify the table prefix, database and database connection information during instantiation;
The strength of the D method is reflected in the strength of your encapsulated custom model classes. However, as the basic model classes of the new ThinkPHP framework become more and more powerful, the M method is more and more practical than the D method.
M method call format:
M ('[basic model name:] model name', 'Data table prefix', 'database connection information ')
Let's take a look at the specific usage of the M method:
1. instantiate the basic Model class
When no model is defined, we can use the following method to instantiate a model class for operations:
// Instantiate the User model $ User = M ('user'); // execute other data operations $ User-> select ();
This method is the simplest and most efficient. Because no model class needs to be defined, cross-project calls are supported. The disadvantage is that there is no custom model class, so you cannot write the relevant business logic and can only perform basic CURD operations.
$User = M('User');
In fact, it is equivalent:
$User = new Model('User');
Operation on the think_user table. The M method and the D method also have the singleton function. Multiple calls will not be instantiated repeatedly. The model name parameter of the M method is automatically converted to lowercase when converted to a data table. That is to say, the data table naming rules of ThinkPHP are in full lowercase format.
2. instantiate other public model classes
The first method of Instantiation is difficult to encapsulate some additional logic methods because there is no definition of the model class. However, in most cases, it may only need to expand some general logic, then you can try the following method.
$User = M('CommonModel:User');
The switching method is actually equivalent:
$User = new CommonModel('User');
Because the system model classes can be automatically loaded, we do not need to manually import class libraries before instantiation. The Model class CommonModel must inherit the Model. We can define some common logical methods in the CommonModel class to avoid defining specific model classes for each data table. If your project has more than 100 data tables, in most cases, there are some basic CURD operations, but some models have complicated business logic to be encapsulated. Therefore, the combination of the first method and the second method is a good choice.
3. input the table prefix, database, and other information.
The M method has three parameters. The first parameter is the model name (which can include the basic model class and database ), the second parameter is used to set the prefix of the data table (if left blank, the prefix of the table configured in the current project is used ), the third parameter is used to set the currently used database connection information (if left blank, the database connection information configured in the current project is used). For example:
$User = M('db2.User','think_');
Instantiate the Model class and operate the think_user table in the db2 database.
If the second parameter is left blank or is not passed, the data table prefix in the current project configuration is used. If the operated data table does not have the table prefix, you can use:
$User = M('db1.User',null);
Instantiate the Model class and operate the user table in the db1 database.
If the database you operate requires different user accounts, you can pass in the database connection information, for example:
$User = M('User','think_','mysql://user_a:1234@localhost:3306/thinkphp');
The base Model class uses the Model, and then performs operations on the think_user table. The user_a account is used to connect to the database, and the operation database is thinkphp.
The third connection information parameter can be configured using DSN or array, or even configuration parameters.
For example, in the project configuration file, configure:
'DB_CONFIG'=>'mysql://user_a:1234@localhost:3306/thinkphp';
You can use:
$User = M('User','think_','DB_CONFIG');
Basic Model classes and databases can be used together, for example:
$User = M('CommonModel:db2.User','think_');
If you want to instantiate a layered model, you can use the public model class:
M('UserLogic:User');
To instantiate UserLogic, although this is of little significance, because it can be used
D('User','Logic');
Implement the same function.
ThinkPHP Function Description: R Method
The R method is used to call the operation method of A controller. It is further enhanced and supplemented by method. For the usage of method A, see here.
Call format of the R method:
R ('[ project: //] [group/] module/operation', 'parameters', 'controller layer name ')
For example, we define an operation method as follows:
class UserAction extends Action { public function detail($id){ return M('User')->find($id); } }
Then you can call this operation method in other controllers using the R method (generally, the R method is used for cross-module calls)
$data = R('User/detail',array('5'));
It indicates that the detail method of the User controller is called (the detail method must be of the public type), and the return value is to query a User data whose id is 5. If the operation method you want to call does not have any parameters, you can leave the second parameter blank and use it directly:
$data = R('User/detail');
Cross-group and project calls are also supported, for example:
R('Admin/User/detail',array('5'));
Call the detail method of the User Controller under the Admin group.
R('Admin://User/detail',array('5'));
Call the detail method of the User Controller under the Admin project.
The official suggestion is not to call too many on the same layer, which may cause logical confusion. The public call part should be encapsulated into a separate interface, and the new features of 3.1 can be used for multi-layer controllers, add a controller layer for interface calling. 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
$data = R('User/detail',array('5'),'Api');
That is to say, the third parameter of the R method supports specifying the called controller layer.
At the same time, when the R method calls the operation method, you can set the operation suffix c ('Action _ SUFFIX '). If you have set the operation method SUFFIX, you do not need to change the calling method of the R method.
The above content will share with you the M method and R method described in ThinkPHP functions, hoping to help you.