Multi-database operation of ThinkPHP3.1 new features more perfect _php instance

Source: Internet
Author: User

In general, if an application simply operates on the same database (or distributed database), you only need to define the database connection information in the project configuration file. The multiple database operations mentioned here generally refer to the operation of different databases (including the same type and different types of databases) in one application, and even the dynamic switching of multiple databases.

For earlier versions of thinkphp, it would take a high-level model to switch databases, and the 3.1 version would be easier to solve.

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

1. Model Definition Database

If it's simply a cross library operation, and it's just an individual model class, you can define the dbname attribute directly in the model class:

protected $dbName = ' top ';

When instantiating, remember to use the D method, for example:

$User = D (' User ');

This approach is defined if the current database user account has permission to manipulate the top database.

2. Model Definition Database connection

If you need to use a different database connection account or need to connect to different types of databases, you can define the connection attribute directly in the model class and automatically connect to the specified database when you manipulate the model class. For example:

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

Or use an 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 '  = > ' 1234 ',
    ' db_host ' => ' localhost ', ' db_port ' => ' 3306 ', ' db_name ' => ' thinkphp '
  ),
  //Database Configuration 2
  ' Db_config2 ' => ' mysql://root:1234@localhost:3306/thinkphp ';

So, we can change the attribute definition of the model class to:

Database configuration in the call configuration file 1
 protected $connection = ' db_config1 ';

Or:

Database configuration in the call configuration file 2
 protected $connection = ' db_config2 ';

The benefits of this approach can support different database types, that is, they can be different from the database types in the current project configuration file, and the disadvantage is that the D method must be instantiated and not dynamically set.

3. Model instantiation Specify Connection

The new version supports specifying a database connection when instantiating a model, for example:

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

or instantiate using the M method:

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

The second parameter of the M method is the prefix of the data table, and if left blank represents the data table prefix with the project configuration, and the third parameter is the database connection information that is currently instantiated.

Similarly, the database connection information that is passed into the instantiation can also take the form of a configuration name, such as:

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

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

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

Represents the Think_user data table for instantiating the top database. If your datasheet has no prefix, you can use the

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

Represents the user table that instantiates the top database.

4. Dynamic Switch Connection

The system also provides more flexible dynamic operations, using the DB method provided by the model class for multiple database connections and switching operations:

MODEL->DB ("Database Number", "Database Configuration");

Database number format, for the database connection has been called, there is no need to pass the database connection information, the system will automatically record. For an initialized database connection, the internal database number is 0, so to avoid conflicts, do not redefine the database configuration with the database number 0.

The database configuration is defined in the same way as the model definition connection property, which supports array, string, and invocation configuration parameters in three formats.

The DB method call returns the current model instance and can proceed directly to other operations of the model, so the method can be dynamically toggled during the query, 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 the database connection information, you can use it directly:

$this->db (1)->query ("Query SQL");

All current operations are the database set up for DB (1) before the database switch is made again.
If you need to switch to the default database connection, you only need to call:

$this->db (0);

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

$this->db (1, "DB_CONFIG1")->query ("Query SQL");
$this->db (2, "Db_config2")->query ("Query SQL");

You can use the table method to specify the data table to be manipulated after the database is switched, and if the datasheet is not currently consistent:

$this->db (1)->table ("Top_user")->find ();

If you want to return to the current database connection, you can call the null DB method directly, 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.