Detailed explanation of the M method and R method of the TP3 function

*文
Release: 2023-03-19 09:08:01
Original
5001 people have browsed it

This article introduces the M method and R method of ThinkPHP3 functions in detail. The M method is used to instantiate a basic model class, while the R method is used to call the operation method of a certain controller. Interested friends follow Script Home Let’s learn together. I hope it will be helpful to everyone.

First of all, I will introduce you to the detailed explanation of ThinkPHP functions: M method

The M method is used to instantiate a basic model class. The difference from the D method is:

1. There is no need to customize the model class, which reduces IO loading and has better performance;

2. After instantiation, only methods in the basic model class (the default is the Model class) can be called;

3. You can specify the table prefix, database and database connection information when instantiating;

The power of the D method is reflected in how powerful the custom model class you encapsulate is , but as the basic model class of the new version of the ThinkPHP framework becomes more and more powerful, the M method is becoming more and more practical than the D method.

The calling format of the M method:

M('[Basic model name:]Model name','Data table prefix','Database connection information')

Let’s take a look at the specific uses of the M method:

1. Instantiate the basic model (Model) class

When no model is defined, we You can use the following method to instantiate a model class for operation:

//实例化User模型
$User = M('User');
//执行其他的数据操作
$User->select();
Copy after login

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 also that there is no custom model class, so the relevant business logic cannot be written and only basic CURD operations can be completed.

$User = M('User');
Copy after login

is actually equivalent to:

$User = new Model('User');
Copy after login

means operating the think_user table. The M method also has a singleton function like the D method, and it will not be instantiated repeatedly if called multiple times. The model name parameter of the M method will be automatically converted to lowercase when converted into a data table, which means that ThinkPHP's data table naming specification is in all lowercase format.

2. Instantiate other public model classes

The first way to instantiate is because there is no definition of the model class, so it is difficult to encapsulate some additional logic. method, but in most cases, you may just need to extend some general logic, then you can try the following method.

$User = M('CommonModel:User');
Copy after login

The changed usage is actually equivalent to:

$User = new CommonModel('User');
Copy after login

Because the system's model classes can be automatically loaded, we do not need to manually import the class library before instantiation. The model class CommonModel must inherit Model. We can define some common logical methods in the CommonModel class, which eliminates the need to define specific model classes for each data table. If your project already has more than 100 data tables, most of them are basic For CURD operations, only some models have some complex business logic that needs to be encapsulated, so the combination of the first method and the second method is a good choice.

3. Incoming table prefix, database and other information

The M method has three parameters. The first parameter is the model name (can include the base model class and database), the second parameter is used to set the prefix of the data table (leave blank to take the table prefix of the current project configuration), and the third parameter is used to set the currently used database connection information (leave it blank to take the current project configuration) database connection information), for example:

$User = M('db2.User','think_');
Copy after login

means instantiating the Model model class and operating the think_user table in the db2 database.

If the second parameter is left blank or not passed, it means using the data table prefix in the current project configuration. If the data table being operated does not have a table prefix, you can use:

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

Indicates instantiating the Model model class and operating the user table in the db1 database.

If the database you operate requires different user accounts, you can pass in the connection information of the database, for example:

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

indicates that the basic model class uses Model, and then operates on the think_user table , use the user_a account to connect to the database, and the operating database is thinkphp.

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

For example, if

'DB_CONFIG'=>'mysql://user_a:1234@localhost:3306/thinkphp';
Copy after login

is configured in the project configuration file, you can use:

$User = M('User','think_','DB_CONFIG');
Copy after login

The basic model class and database can be used together, for example:

$User = M('CommonModel:db2.User','think_');
Copy after login

If you want to instantiate a hierarchical model, using the public model class, we can use:

M('UserLogic:User');
Copy after login

to instantiate UserLogic, although this does not make much sense. , because the same function can be achieved with

D('User','Logic');
Copy after login

.

Detailed explanation of ThinkPHP functions: R method

The R method is used to call the operation method of a certain controller, which is a further enhancement and supplement of the A method. See here for the usage of method A.

R method calling format:

R('[Project://][Group/]Module/Operation','Parameter','Controller layer name')

For example, we define an operation method as:

class UserAction extends Action {
 public function detail($id){
  return M('User')->find($id);
 }
 }
Copy after login

Then you can call this operation method in other controllers through the R method (generally the R method is used for cross-module calls)

$data = R('User/detail',array('5'));
Copy after login

表示调用User控制器的detail方法(detail方法必须是public类型),返回值就是查询id为5的一个用户数据。如果你要调用的操作方法是没有任何参数的话,第二个参数则可以留空,直接使用:

$data = R('User/detail');
Copy after login

也可以支持跨分组和项目调用,例如:

R('Admin/User/detail',array('5'));
Copy after login

表示调用Admin分组下面的User控制器的detail方法。

R('Admin://User/detail',array('5'));
Copy after login

表示调用Admin项目下面的User控制器的detail方法。

官方的建议是不要在同一层多太多调用,会引起逻辑的混乱,被公共调用的部分应该封装成单独的接口,可以借助3.1的新特性多层控制器,单独添加一个控制器层用于接口调用,例如,我们增加一个Api控制器层,

class UserApi extends Action {
 public function detail($id){
  return M('User')->find($id);
 }
 }
Copy after login

然后,使用R方法调用

$data = R('User/detail',array('5'),'Api');
Copy after login

也就是说,R方法的第三个参数支持指定调用的控制器层。

同时,R方法调用操作方法的时候可以支持操作后缀设置C('ACTION_SUFFIX'),如果你设置了操作方法后缀,仍然不需要更改R方法的调用方式。

相关推荐:

详解ThinkPHP5 instance的实现

ThinkPHP下表单令牌错误的分析与解决

详解ThinkPHP如何实现生成和校验验证码

The above is the detailed content of Detailed explanation of the M method and R method of the TP3 function. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template