Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the m method of thinkphp

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use the m method of thinkphp. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

In thinkphp, the M () method is used to instantiate a base model class, and after instantiation, you can only call methods in the underlying model class (default is the Model class) with the syntax "M ('[base model name:] model name', 'data table prefix', 'database connection information')".

The operating environment of this tutorial: Windows7 system, thinkphp v5.1, Dell G3 computer.

The M method is used to instantiate a base model class, which differs from the D method in:

No need for custom model class, reduce IO load, better performance

Only methods in the underlying model class (default is Model class) can be called after instantiation

You can specify table prefixes, database and database connection information when instantiating

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

The calling format of the M method:

M ('[base 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 basic model (Model) class

When no model is defined, we can instantiate a model class to operate 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 there is no need to define any model classes, so cross-project calls are supported. The disadvantage is also that there is no custom model class, so the relevant business logic can not be written, only basic CURD operations can be completed.

$User = M ('User')

In fact, it is equivalent to:

$User = new Model ('User')

Indicates that the think_ user table is manipulated. The M method has the same singleton function as the D method, and multiple calls are not repeatedly instantiated. The model name parameter of the M method is automatically converted to lowercase when converted to a data table, which means that ThinkPHP's data table naming convention is in all lowercase format.

2. Instantiate other common model classes

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

$User = M ('CommonModel:User')

The change of 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 import the class library before instantiating. The model class CommonModel must inherit Model. We can define some general logic methods in the CommonModel class, without defining specific model classes for each data table, if your project already has more than 100 data tables, and in most cases are some basic CURD operations, but individual models have some complex business logic to encapsulate, then the combination of the first way and the second way is a good choice.

3. Input 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), and 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). The third parameter is used to set the currently used database connection information (leave blank to take the database connection information configured by the current project), for example:

$User = M ('db2.User','think_')

Represents an instantiated Model model class and manipulates the think_ user table in the db2 database.

If the second parameter is left blank or not passed, it means that the data table prefix in the current project configuration is used. If the data table you are operating on does not have a table prefix, you can use:

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

Represents an instantiated Model model class and manipulates the user table in the db1 database.

If the database you are operating 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')

Indicates that the basic model class uses Model, then operates on the think_ user table, uses the user_a account to connect to the database, and operates the database as thinkphp.

The third connection information parameter can be configured using DSN or array configuration, or even support configuration parameters.

For example, in the project configuration file, you configured:

'DB_CONFIG'= >' mysql://user_a:1234@localhost:3306/thinkphp'

You can use:

$User = M ('User','think_','DB_CONFIG')

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

$User = M ('CommonModel:db2.User','think_')

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

M ('UserLogic:User')

To instantiate UserLogic, although it doesn't make much sense because you can use the

D ('User','Logic')

To achieve the same function.

This is the end of the article on "how to use the m method of thinkphp". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report