Solution of Multi database Connection Based on ThinkPHP

  • 2021-07-07 06:40:37
  • OfStack

When ThinkPHP realizes connecting multiple data, if the database is in the same server, it only needs to define the model as follows:


class MembersModel extends Model{
protected $trueTableName = 'members.members'; // Database name . Table name ( Includes the prefix )
}

Then it can be like D ("Members"); In this way, the model is instantiated and operated like a normal model.
But it turned out that his database was on two different servers, so the above method didn't work.
At this time, it is necessary to use the multi-data connection feature of TP.

In this regard, after consulting the official documents for testing and correction, the following solutions are obtained:

To establish a multi-data connection, you must first construct database configuration parameters. But if you set up a database configuration array every time you set up a multi-database connection, it will be very troublesome, so it is better to write it in the configuration file. How to write here still needs some skills.


<?php
$config= array(
'DEBUG_MODE'=>true,
'default_module'=>'Index',
'ROUTER_ON'=>TRUE,
'DATA_RESULT_TYPE'=>1,
'SHOW_RUN_TIME'=>true,   //  Runtime display 
'SHOW_ADV_TIME'=>true,   //  Display detailed running time 
'SHOW_DB_TIMES'=>true,   //  Display database queries and writes 
'SHOW_CACHE_TIMES'=>true,  //  Displays the number of cache operations 
'SHOW_USE_MEM'=>true,   //  Show memory overhead 
'HTML_FILE_SUFFIX'=>'.shtml',  //  Default static file suffix 
'HTML_CACHE_ON' =>false,   //  Static caching is turned off by default 
'HTML_CACHE_TIME'=>60,   //  Static cache validity period 
'HTML_READ_TYPE'=>1,   //  Static cache read mode  0 readfile 1 redirect
'HTML_URL_SUFFIX'=>'.shtml', //  Pseudo static suffix setting 
// Default database link 
'DB_TYPE'=>'mysql',
'DB_HOST'=>'localhost',
'DB_NAME'=>'news',
'DB_USER'=>'root',
'DB_PWD'=>'123',
'DB_PORT'=>'3306',
'DB_PREFIX'=>'news_',
// My first 1 Database connections 
'DB_BBS'=>array(
'dbms' => 'mysql',
'username' => 'discuz',
'password' => '123',
'hostname' => 'localhost',
'hostport' => '3306',
'database' => 'discuz'
),
// No. 1 2 Database links ,
'DB_NEWS'=>array(
'dbms'=>'mysql',
'username'=>'root',
'password'=>'123',
'hostname'=>'localhost',
'hostport'=>'3306',
'database'=>'news'
)
);
return $config;
?>

At this point, we can use C ("DB_BBS") and C ("DB_NEWS") to get the configuration array of the database.
Now that you have configured it, you need to instantiate the model. Because our model uses two different database connections, the project configuration file defaults to a database configuration, if you model a table such as UserModel. class. php,
If you use D ("User"); However, if there are no User tables in the current default database, an error will be reported. So we need to build an empty model. No table will be selected for an empty model.
There are two ways to build an empty model. $dao=D (); And $dao=new Model (); Either way.


$dao=D();

After instantiating the model, we need to add the database model;


$dao->addConnect(C("DB_BBS"),1,true);
$dao->addConnect(C("DB_NEWS"),2,true);

Say 1 this addConnect (); The prototype of this function is different in 1.0. 3 and 1.0. 4.
The prototype in 1.0. 3 is:


boolean addConnect (mixed $config, mixed $linkNum, [boolean $eqType = true])

The prototype in 1.0. 4 is:


boolean addConnect (mixed $config, mixed $linkNum)

The third parameter is missing.
The first parameter is the configuration array of the database, and the second parameter is the number of the added connection, which needs to give the connection with which serial number when switching database connections. Note that the built-in database connection sequence number is 0, so the additional database connection sequence number should start from 1. The third parameter is true if the two databases are the same connection;

After adding the database connection, you can switch the database connection at any time. For example, we want to use the database DB_NEWS, just write this:


$dao->switchConnect(2);

Because only the database connection is established here, and there is no table selection, the next step is to select the table.
Note that the table name here is the full name, that is, the prefix of the table plus the table name. Because we have no prefix in the configuration array of the connection database. I think it should be definable, but I don't know. That's it now.


$dao->table("cdb_members");

After that, you can use this model like ordinary model 1.
For example, I want to query all the information of ID users passed through:


$map=array("id"=>$_GET["id"]);
$res=$dao->find($map);

You can see if the query is successful.


dump($res);

If you want to use the table of DB_BBS database now, you only need to switch the connection once more;


<?php
$config= array(
'DEBUG_MODE'=>true,
'default_module'=>'Index',
'ROUTER_ON'=>TRUE,
'DATA_RESULT_TYPE'=>1,
'SHOW_RUN_TIME'=>true,   //  Runtime display 
'SHOW_ADV_TIME'=>true,   //  Display detailed running time 
'SHOW_DB_TIMES'=>true,   //  Display database queries and writes 
'SHOW_CACHE_TIMES'=>true,  //  Displays the number of cache operations 
'SHOW_USE_MEM'=>true,   //  Show memory overhead 
'HTML_FILE_SUFFIX'=>'.shtml',  //  Default static file suffix 
'HTML_CACHE_ON' =>false,   //  Static caching is turned off by default 
'HTML_CACHE_TIME'=>60,   //  Static cache validity period 
'HTML_READ_TYPE'=>1,   //  Static cache read mode  0 readfile 1 redirect
'HTML_URL_SUFFIX'=>'.shtml', //  Pseudo static suffix setting 
// Default database link 
'DB_TYPE'=>'mysql',
'DB_HOST'=>'localhost',
'DB_NAME'=>'news',
'DB_USER'=>'root',
'DB_PWD'=>'123',
'DB_PORT'=>'3306',
'DB_PREFIX'=>'news_',
// My first 1 Database connections 
'DB_BBS'=>array(
'dbms' => 'mysql',
'username' => 'discuz',
'password' => '123',
'hostname' => 'localhost',
'hostport' => '3306',
'database' => 'discuz'
),
// No. 1 2 Database links ,
'DB_NEWS'=>array(
'dbms'=>'mysql',
'username'=>'root',
'password'=>'123',
'hostname'=>'localhost',
'hostport'=>'3306',
'database'=>'news'
)
);
return $config;
?>
0

Then select table query. Remember, after switching the model, you must select the table again, otherwise you will make an error.
Then you can operate like a normal model.
The following points out several problems in the manual:

1. When instantiating multi-database connection, a non-empty model is established. It seems to be wrong. ) This could lead to errors. It is suggested to establish an empty model.
2. The parameters of 2. addConnect () are different in different versions, which are not written in the manual;
3. After establishing an empty model, you need to select a table, which is not in this manual.

In view of the above points, ThinkPHP users can make corresponding adjustments according to different versions.

Readers who are interested in thinkPHP can check the topics of this site: "Introduction to ThinkPHP", "Summary of thinkPHP Template Operation Skills", "Summary of ThinkPHP Common Methods", "Basic Introduction to smarty Template" and "Summary of PHP Template Technology".

I hope this article is helpful to the PHP programming based on ThinkPHP framework.


Related articles: