ThinkPHP3.1 new features comprehensive multi-database operations

Source: Internet
Author: User
Under normal circumstances, if the application only operates the same database (or distributed database), you only need to define the database connection information in the project configuration file, multi-database operations here generally refer to operations on different databases in an application.

ThinkPHP3.1 new features comprehensive multi-database operations

Under normal circumstances, if the application only operates the same database (or distributed database), you only need to define the database connection information in the project configuration file, the multi-database operation mentioned here generally refers to operations on different databases (including the same type and different types of databases) in an application, and even the dynamic switching of multiple databases.

In earlier versions of ThinkPHP, the advanced model is required to switch databases. now it is easier to solve this problem.

The new version supports multiple data in the following ways. developers can select an appropriate method based on the actual situation:

Model Definition Database

If it is just a simple cross-database operation and only a few model classes, you can directly define the dbName attribute in the model class:

Protected $ dbName = 'top ';

Remember to use the D method when instantiating, for example:

$ User = D ('user ');

The premise of this method is that the current database user account has the permission to operate the top database.

Model Definition Database Connection

If you need to use different database connection accounts or connect to different types of databases for cross-database operations, you can directly define the connection attribute in the model class. when operating on the model class, will automatically connect to the specified database, for example:

Protected $ connection = 'MySQL: // root: 1234 @ localhost: 3306/thinkphp ';

Or use the array method to define:

Protected $ connection = array ('Db _ type' => 'mysql', 'DB _ user' => 'root', 'DB _ pwd' => '123 ', 'db _ host' => 'localhost', 'DB _ port' => '000000', 'DB _ name' => 'thinkphp ');

If we have configured additional database connection information in the configuration file, for example:

  1. // Database configuration 1 'DB _ config1' = array ('Db _ type' => 'mysql', 'DB _ user' => 'root ', 'db _ pwd' => '123', 'DB _ host' => 'localhost', 'DB _ port' => '123 ', 'db _ name' => 'thinkphp'), // database configuration 2 'DB _ config2' => 'MySQL: // root: 1234 @ localhost: 3306/thinkphp ';
  2. Then, we can change the attribute definition of the model class:
  3. // Call the database configuration 1 protected $ connection = 'DB _ config1' in the configuration file ';
  4. // Or:
  5. // Call the database configuration 2 protected $ connection = 'DB _ config2' in the configuration file ';

This method supports different database types, that is, it can be different from the database types in the current project configuration file. The disadvantage is that the D method must be instantiated and cannot be dynamically set.

Model instantiation specified connection

The new version supports specifying database connections during model instantiation, for example:

$ User = new Model ('user', 'think _ ', 'MySQL: // root: 1234 @ localhost/thinkphp ');

Or use the M method for instantiation:

$ User = M ('user', 'think _ ', 'MySQL: // root: 1234 @ localhost/thinkphp ');

The second parameter of the M method is the prefix of the data table. if it is left blank, the data table prefix configured by the project is used. The third parameter is the database connection information required for the current instantiation.

Similarly, the database connection information passed in the instantiation can also be configured by name, for example:

$ User = M ('user', 'think _ ', 'DB _ config2 ');

If you do not need to switch the database connection for the current operation, you can use:

$ User = M ('Top. user', 'think _');

The think_user data table of the top database is instantiated. if your data table does not have a prefix, you can use

$ User = M ('Top. user', null );

Indicates the user table of the top database to be instantiated.

Dynamically switch connections

The system also provides more flexible dynamic operations. you can use the db method provided by the model class for multi-database connection and switching operations. usage:

Model-> db ("database number", "database configuration ");

Database numbers are in the numeric format. for database connections that have been called, you do not need to input database connection information. The system automatically records the database connections that have been initialized, the internal database number is 0. to avoid conflicts, do not define the database configuration with the database number as 0 again.

The definition method of database configuration is the same as that of model definition connection attribute. three formats are supported: Array, string, and call configuration parameter.

After the Db method is called, the current model instance is returned, and other model operations can be performed directly. Therefore, this method can be dynamically switched during the query process, for example:

$ This-> db (1, "mysql: // root: 123456 @ localhost: 3306/test")-> query ("query SQL ");

This method adds a database connection numbered 1 and automatically switches to the current database connection.

When you switch to the same database for the second time, you do not need to input the database connection information. you can directly use:

$ This-> db (1)-> query ("query SQL ");

Before the database switch is performed again, all current operations are performed on the database set for db (1.

To switch to the default database connection, you only need to call:

$ This-> db (0 );

If we have defined other database connection information in the project configuration, we can directly call the configuration in the db Method for connection:

$ This-> db (1, "DB_CONFIG1")-> query ("query SQL"); $ this-> db (2, "DB_CONFIG2 ") -> query ("query SQL ");

If the data table is inconsistent with the current one after the database is switched, you can use the table method to specify the data table to be operated:

$ This-> db (1)-> table ("top_user")-> find ();

To return the current database connection, you can directly call the empty db Method. for example:

$ Db = $ this-> db ();

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.