New features of ThinkPHP 3.1 are more sophisticated in multi database operations

  • 2021-07-02 23:39:40
  • OfStack

Normally, if an application operates only on the same database (or distributed database), you only need to define the database connection information in the project configuration file. The multi-database operation 1 generally refers to the operation of different databases (including the same type and different types of databases) in an application, and even includes the dynamic switching of multi-databases.

For earlier versions of ThinkPHP, switching databases required the use of an advanced model, whereas 3.1 now makes it easier to do so.

The new version supports multiple data in the following ways, and developers can choose the appropriate way to operate according to the actual situation:

1. Model definition database

If it is a simple cross-library operation and only an individual model class, you can directly define the dbName attribute in the model class:


protected $dbName = 'top';

When instantiating, remember to use the D method, such as:


$User = D('User');

This definition assumes that the current database user account has access to the top database.

2. The model defines the database connection

If your cross-library operation needs to use different database connection accounts or connect to different types of databases, you can directly define connection attributes in the model class, and when operating the model class, it will automatically connect to the specified database. For example:


protected $connection = 'mysql://root:1234@localhost:3306/thinkphp';

Or use an array 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'  => '1234',
    'db_host' => 'localhost',
    'db_port' => '3306',
    '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 to:


// Invoke the database configuration in the configuration file 1
 protected $connection = 'DB_CONFIG1';

Or:


// Invoke the database configuration in the configuration file 2
 protected $connection = 'DB_CONFIG2';

The advantage of this approach is that it can support different database types, that is, it can be different from the database types in the current project configuration file. The disadvantage is that it must be instantiated using D method and cannot be dynamically configured.

3. Model instantiation specifies the connection

The new version supports specifying database connections when instantiating the model, such as:


$User = new Model('User','think_','mysql://root:1234@localhost/thinkphp'); 

Or use the M method to instantiate:


$User = M('User','think_','mysql://root:1234@localhost/thinkphp'); 

The second parameter of the M method is the prefix of the data table. If left blank, it means that the data table prefix configured by the project is adopted, and the third parameter is the database connection information required for the current instantiation.

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


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

If the current operation does not need to switch database connections, but only needs to switch databases, you can use:


$User = D('User');

0

Represents an think_user data table that instantiates an top database. If your data table does not have a prefix, you can use


$User = D('User');

1

Represents an user table that instantiates an top database.

4. Switch connections dynamically

The system also provides more flexible dynamic operation, and can use db method provided by model class to connect and switch multiple databases. Usage:


$User = D('User');

2

The database number is in digital format. For the database connection that has been called, there is no need to pass in the database connection information, and the system will automatically record it. For initialized database connections, the internal database number is 0, so to avoid conflicts, do not redefine the database configuration with database number 0.

Database configuration definition and model definition connection attribute 1 sample, support array, string and call configuration parameters 3 formats.

After the Db method is called, it returns the current model instance and can directly continue other operations of the model, so the 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 don't need to pass in database connection information, you can directly use:


$User = D('User');

4

Until the database is switched again, all current operations are against the database set up by db (1).
If you need to switch to the default database connection, just call:


$User = D('User');

5

If we have defined additional database connection information in the project configuration, we can call the configuration directly in the db method to connect:


$User = D('User');

6

If the data table and the current data table are not the same after switching the database, you can use the table method to specify the data table to operate on:


$User = D('User');

7

If you want to return the current database connection, you can directly call the empty db method, for example:


$db = $this->db();

Related articles: