In the previous article, we summarized the new changes in ThinkPHP3.2. in this article, we will take a detailed look at the new features of the database, which are very detailed. if you need them, refer to them. The database driver of ThinkPHP3.2.3 is completely rewritten using PDO. the configuration and usage are more flexible and powerful than the previous version. let's take a look at how to use it.
First, the database configuration information of 3.2.3 has been adjusted. The Complete database settings include:
The code is as follows:
/* Database settings */
'Db _ type' => '', // Database TYPE
'Db _ host' => '', // server address
'Db _ name' => '', // database NAME
'Db _ user' => '', // USER name
'Db _ pwd' => '', // password
'Db _ port' => '', // PORT
'Db _ prefix' => '', // database table PREFIX
'Db _ PARAMS '=> array (), // database connection parameters
'Db _ debug' => TRUE, // you can record SQL logs after enabling the database debugging mode.
'Db _ LITE '=> false, // use the Lite mode of the database.
'Db _ FIELDS_CACHE '=> true, // enable field caching
'Db _ charset' => 'utf8', // The database encoding uses utf8 by default.
'Db _ DEPLOY_TYPE '=> 0, // Database deployment method: 0 centralized (single server), 1 distributed (master/slave server)
'Db _ RW_SEPARATE '=> false, // whether the master-slave mode is valid for database read/write splitting
'Db _ MASTER_NUM '=> 1, // number of master servers after read/write splitting
'Db _ slave_no' => '', // specify the serial number of the slave server
The following parameters are removed from version 3.2.2:
The code is as follows:
'Db _ FIELDTYPE_CHECK '// 3.2.3 forcibly checks the field type.
'Db _ SQL _BUILD_CACHE '// 3.2.3 cancels SQL creation cache
'Db _ SQL _BUILD_QUEUE '// 3.2.3 cancels SQL creation cache
'Db _ SQL _BUILD_LENGTH '// 3.2.3 cancels SQL creation cache
'Db _ SQL _LOG '// replaced by the new DB_DEBUG parameter
'Db _ BIND_PARAM '// The new version uses PDO to automatically bind parameters without setting
The new database settings include:
The code is as follows:
'Db _ debug' // used to enable the database debugging mode. after enabling this mode, you can record SQL logs.
'Db _ LITE '// whether to enable the Database Lite mode connection. only native SQL queries can be used.
3.2.2 The debugging mode of the database version and the debugging mode of the project (defined by the APP_DEBUG constant) are bound. the debugging mode of the database version starting from 3.2.3 is set independently (set by the DB_DEBUG parameter.
The DB_TYPE parameter is used to set the database type. Currently, the supported drivers include mysql, sqlite, oracle, pgsql, sqlsrv, and firebird (drivers need to be added for other database types). The settings are as follows:
'Db _ type' => 'mysql', // it cannot be set to PDO or be different from mysql or mysqli.
Database Connection information, including the following parameters:
The code is as follows:
'Db _ host' => '', // IP address used for the server address
'Db _ name' => '', // database NAME
'Db _ user' => '', // USER name
'Db _ pwd' => '', // password
'Db _ port' => '', // if the PORT is left empty, the default PORT is used.
'Db _ charset' => '', // database encoding
The preceding parameters are automatically converted to the PDO connection parameters when the PDO instance is instantiated.
DB_DSN parameters generally do not need to be set. the system's database driver will perform the default settings. if you need to adjust the parameters, follow the DSN settings of the relevant database connection of PDO.
DB_PARAMS is used to set database connection parameters. The fourth parameter of PDO instantiation is passed in.
The following is a typical global database configuration:
The code is as follows:
'Db _ type' => 'mysql', // Database TYPE
'Db _ host' => '192. 168.1.10 ', // server address
'Db _ name' => 'thinkphp', // database NAME
'Db _ user' => 'root', // USER name
'Db _ pwd' => '123', // password
'Db _ port' => '123', // PORT
'Db _ prefix' => 'think _ ', // database table PREFIX
'Db _ charset' => 'utf8', // database encoding
'Db _ debug' => TRUE, // you can record SQL logs after enabling the database debugging mode.
If you set a separate connection attribute for database connection information in the model class, you can use the following array or string method:
The code is as follows:
// Set the database connection information separately in the model
Namespace Home \ Model;
Use Think \ Model;
Class UserModel extends Model {
// Defined by array
Protected $ connection = array (
'Db _ type' => 'mysql ',
'Db _ user' => 'root ',
'Db _ pwd' => '123 ',
'Db _ host' => '2017. 168.1.10 ',
'Db _ port' => '123 ',
'Db _ name' => 'thinkphp ',
'Db _ charset' => 'utf8 ',
);
}
Note: The database connection setting parameters set in the model use the globally configured lowercase name.
You can also define it as a string in the following format:
Database type: // user name: password @ database address: database port/database name # Character set
For example:
The code is as follows:
// Set the database connection information separately in the model
Namespace Home \ Model;
Use Think \ Model;
Class UserModel extends Model {
// Define using strings
Protected $ connection = 'MySQL: // root: 1234@192.168.1.10: 3306/thinkphp # utf8 ';
}
You can also set it through the configuration file, for example:
The code is as follows:
// Database configuration 1
'Db _ config1' => array (
'Db _ type' => 'mysql ',
'Db _ user' => 'root ',
'Db _ pwd' => '123 ',
'Db _ host' => '2017. 168.1.10 ',
'Db _ port' => '123 ',
'Db _ name' => 'thinkphp ',
'Db _ charset' => 'utf8 ',
),
// Database configuration 2
'Db _ config2' => 'MySQL: // root: 1234@192.168.1.10: 3306/thinkphp # utf8 ';
Then define in the model:
The code is as follows:
// Set the database connection information separately in the model
Namespace Home \ Model;
Use Think \ Model;
Class UserModel extends Model {
// Call the database configuration 1 in the configuration file
Protected $ connection = 'DB _ config1 ';
// Or
Protected $ connection = 'DB _ config2 ';
}
In addition to specifying database connection information during Model definition, we can also specify database connection information during instantiation. if the M method is used to instantiate the model, you can also pass in different database connection information, for example:
The code is as follows:
$ User = M ('user', 'other _ ', 'MySQL: // root: 1234@192.168.1.10/demo # utf8 ');
The User model is instantiated and connected to the other_user table of the demo database. the connection information used is configured by the third parameter.
If DB_CONFIG2 has been configured in the project configuration file, you can also use:
$ User = M ('user', 'other _ ', 'DB _ config2 ');
The above is all the content of this article. I hope you will like it.