In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how to use the m method in thinkphp5.0, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this thinkphp5.0 article on how to use the m method. Let's take a look at it.
In thinkphp, the m method is used to instantiate a base model class, and the m method instantiates a Model object dynamically by instantiating the Model method directly, even if the corresponding Model file does not exist, the syntax is "$User=M (model name, data table prefix, currently used database connection information);".
This article operating environment: Windows10 system, ThinkPHP5 version, Dell G3 computer.
How to use m method in thinkphp5.0
The M method is used to instantiate a base model class, which differs from the D method in:
1. Do not need custom model classes, reduce IO loading, and have better performance
2. After instantiation, you can only call methods in the underlying model class (default is Model class).
3. You can specify the table prefix, 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 in thinkphp5.0". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use m method in thinkphp5.0". If you want to learn more, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.