thinkphp 3.2 Simple Method for Connecting Two Databases Simultaneously
- 2021-12-13 16:36:34
- OfStack
Because of the project requirements, it is necessary to connect two libraries, so a method is encapsulated, and the example is as follows:
tp 3.2 Manual Connection Database
Method of adding encapsulation to common function. php
function getCrmModel($name)
{
// new Adj. model You can change it according to your own needs
return new \Home\Model\CrmModel($name);
}
model code
<?php
namespace Home\Model;
class CrmModel extends BaseModel
{
public function _initialize(){
parent::_initialize();
$this->connection = array(
'db_type' => C('CRM_DB.DB_TYPE'),
'db_user' => C('CRM_DB.DB_USER'),
'db_pwd' => C('CRM_DB.DB_PWD'),
'db_host' => C('CRM_DB.DB_HOST'),
'db_port' => C('CRM_DB.DB_PORT'),
);
$this->dbName = C('CRM_DB.DB_NAME');
$this->tablePrefix = C('CRM_DB.DB_PREFIX');
}
}
Adding configuration information in the common config
// No. 1 2 Database configuration information
'SFK_DB'=>array(
'DB_TYPE' => 'mysql', // Database type
'DB_HOST' => 'localhost', // Server address
'DB_NAME' => 'test', // Database name
'DB_USER' => 'root', // User name
'DB_PWD' => 'root', // Password
'DB_PORT' => '3306', // Port
'DB_PREFIX' => 'test_', // Prefix
),
Summarize