thinkphp functions in detail: M method, thinkphp function detailed _php tutorial

Source: Internet
Author: User
Tags db2

thinkphp function Description: M method, thinkphp function detailed


The M method is used to instantiate a base model class, and the difference between the D method is:
1, do not need to customize the model class, reduce IO loading, good performance;
2, after instantiation can only invoke the Basic model class (the default is the Model Class) in the method;
3, you can specify the table prefix, database and database connection information at the time of instantiation;
The power of the D method is reflected in how strong the custom model classes you encapsulate, but as the new thinkphp framework's underlying model classes become more powerful, the M method is more practical than the D method.
The invocation format of the M method:
M (' [Base model name:] Model name ', ' data table prefix ', ' database connection information ')
Let's look at how the M method is used specifically:
1. Instantiation of the basic model class

When no model is defined, we can instantiate a model class using the following method:

Instantiate the User model $user = M (' User ');//Perform other data operations $user->select ();

This approach 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 because there is no custom model class, the related business logic cannot be written and only basic curd operations can be done.

$User = M (' User ');

is actually equivalent to:

$User = new Model (' User ');

Represents the Action Think_user table. The M method has a singleton function as well as the D method, and multiple invocations do not repeat the instantiation. The model name parameter of the M method is automatically converted to lowercase when converted to a data table, which means that the thinkphp table naming specification is in all lowercase format.


2. Instantiation of other public model classes

The first method is instantiated because there is no definition of the model class, so it is difficult to encapsulate some additional logical methods, but in most cases, perhaps just need to extend some common logic, then you can try one of the following methods.

$User = M (' Commonmodel:user ');

The change 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 perform class library import operations prior to instantiation. Model class Commonmodel must inherit models. We can define some common logic methods in the Commonmodel class, and we can omit to define a specific model class for each data table, if your project already has more than 100 data tables, and most of the cases are some basic curd operations, Just the individual models have some complex business logic that needs to be encapsulated, so the combination of the first and second approaches is a good choice.


3. Incoming 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 database), the second parameter sets the data table prefix (leaving the table prefix for the current project configuration blank), and the third parameter sets the database connection information currently in use (leaving the database connection information for the current project configuration blank). For example:

$User = M (' DB2. User ', ' think_ ');

Represents the instantiation of the model class and the operation of the Think_user table in the DB2 database.
If the second argument is left blank or does not pass, the data table prefix in the current project configuration is used, and if the data table for the operation does not have a table prefix, then you can use:

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

Represents the instantiation of the model class and the operation of the user table in the DB1 database.
If you are working on a database that 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 ');

The basic model class is represented by the models, then the Think_user table is manipulated, the database is connected with the User_a account, and the operation database is thinkphp.
The third connection information parameter can use a DSN configuration or an array configuration, and can even support configuration parameters.
For example, in the project configuration file, configure the following:

' Db_config ' = ' mysql://user_a:1234@localhost:3306/thinkphp ';

You can use:

$User = M (' User ', ' think_ ', ' db_config ');

The underlying model classes and databases can be used together, for example:

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

If you are instantiating a hierarchical model, using the public model class, we can use:

M (' Userlogic:user ');

To instantiate the Userlogic, although this does not make much sense because it can be used

D (' User ', ' Logic ');

Achieve the same functionality.

http://www.bkjia.com/PHPjc/1053809.html www.bkjia.com true http://www.bkjia.com/PHPjc/1053809.html techarticle thinkphp function in detail: M method, thinkphp function detailed M method is used to instantiate a basic model class, and the D method is different: 1, do not need to customize the model class, reduce IO loading, sex ...

  • 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.