ThinkPHP M method example _ php instance

Source: Internet
Author: User
This article mainly introduces the m method of ThinkPHP. If you need the M method, you can refer to the M method to instantiate a basic model class, Difference from method DIs:

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.