ThinkPHP3.1 new features: improved database operations

Source: Internet
Author: User

Generally, if an 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.

For earlier versions of ThinkPHP, the advanced model is required to switch databases, while the current version 3.1 can be more easily solved.

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

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

2. 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'  => '1234',  'db_host' => 'localhost',  'db_port' => '3306',  'db_name' => 'thinkphp' );

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

// 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 ';

Then, we can change the attribute definition of the model class:

// Call the Database Configuration 1 protected $ connection = 'db _ config1' in the configuration file ';


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

3. Specify the connection for model instantiation

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.

4. dynamically switch the connection

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 ");

The database number is in the numeric format. For database connections that have been called, you do not need to input the database connection information. The system will automatically record the information. For the initialized database connection, the internal database number is 0. To avoid conflicts, do not define the database configuration with the database number 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:


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:


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