Home > PHP Framework > ThinkPHP > body text

How to use m method in thinkphp5.0

WBOY
Release: 2022-02-15 14:39:57
Original
2603 people have browsed it

In thinkphp, the m method is used to instantiate a basic model class. The m method dynamically instantiates a Model object by directly instantiating the Model method, even if the corresponding Model file does not exist. Syntax It is "$User=M (model name, data table prefix, currently used database connection information);".

How to use m method in thinkphp5.0

The operating environment of this article: Windows 10 system, ThinkPHP5 version, Dell G3 computer.

How to use the m method in thinkphp5.0

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

1. No customization is required Model class reduces IO loading and has better performance;

2. Only methods in the basic model class (the default is the Model class) can be called after instantiation;

3. It can be instantiated When specifying the table prefix, database and database connection information; the power of the

D method is reflected in how powerful the custom model class you encapsulate is. However, as the functions of the basic model class of the new version of ThinkPHP framework become more and more powerful, It is becoming more and more powerful, and the M method is more and more practical than the D method.

The calling format of the M method:

M('[基础模型名:]模型名','数据表前缀','数据库连接信息')
Copy after login

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 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 there is no need to define any model class, so it is supported 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 logical methods, but in most cases, maybe If you just need to extend some general logic, 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. Pass in table prefix, database and other information

The M method has three parameters. The first parameter is the model name (can include basic model classes and databases), and the second parameter is Used to set the prefix of the data table (leave it 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 database connection information of the current project configuration), 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

represents an instance Transform the Model 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 connection information of the database, for example:

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

represents the basic model class using Model, and then to operate the think_user table, use user_a The account is used 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 we 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 we can use

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

Achieve the same function.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to use m method in thinkphp5.0. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!