ThinkPHP3.2.3 Database Setting new features

Source: Internet
Author: User
Tags string format string methods

In the previous article, we have summed up the new changes in the next ThinkPHP3.2, this article we will look at the database on what the new features, very meticulous, the need for small partners under the reference.

-->

The ThinkPHP3.2.3 version database driver is fully rewritten with PDO, configuration and usage is more flexible and powerful than previous versions, so let's understand how to use them.

First, the 3.2.3 database configuration information has been adjusted, and 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 ' => ',//username
' Db_pwd ' => ',//password
' Db_port ' => ',//Port
' Db_prefix ' => ',//database table prefix
' Db_params ' => Array (),//database connection parameters
' Db_debug ' => TRUE,//SQL log can be logged after database debug mode is turned on
' Db_lite ' => false,//using Database LITE mode
' Db_fields_cache ' => true,//enable field caching
' Db_charset ' => ' UTF8 ',//database encoding defaults to use UTF8
' Db_deploy_type ' => 0,//Database Deployment mode: 0 Centralized (single server), 1 distributed (Master-slave server)
' Db_rw_separate ' => false,//database read/write separate master-slave valid
' Db_master_num ' => 1,//read/write separated rear server number
' Db_slave_no ' => ',//specify from server serial number

As opposed to version 3.2.2, the following setting parameters were canceled:

The code is as follows:
' Db_fieldtype_check '//3.2.3 force field type detection
' 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 '//new version adopts PDO automatic parameter binding without setting

The new database setup parameters include:

The code is as follows:
' Db_debug '///used to open database debug mode and log SQL logs upon opening
' Db_lite '/whether to use the database LITE mode connection to open only the native SQL query

The debug mode of the 3.2.2 version database and the debug mode of the project (defined by the App_debug constant) are bound and the debug mode of the 3.2.3 Start Database is set independently (set by the Db_debug parameter).

The Db_type parameter is set for the database type, and the driver currently supported includes the Mysql/sqlite/oracle/pgsql/sqlsrv/firebird (other database types need to be increased), set as follows:
' Db_type ' => ' mysql ',//no longer support settings for PDO also no longer distinguish between MySQL and mysqli
Copy Code
Database connection information, mainly includes the following parameters:

The code is as follows:
' Db_host ' => ',//server address with IP address
' Db_name ' => ',//database name
' Db_user ' => ',//username
' Db_pwd ' => ',//password
' Db_port ' => ',//port left blank to take default port
' Db_charset ' => ',//Database encoding

The above setting parameters are automatically converted to the PDO connection parameters when the PDO is instantiated.

DB_DSN parameters generally do not need to set, the system's database driver will be the default settings, if you need to adjust, please follow the PDO related database connection DSN settings to set.

Db_params is used to set the connection parameters for the database, passing in the fourth parameter of the PDO instantiation.

The following is a typical database global setting:

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 ',//username
' Db_pwd ' => ' 1234 ',//password
' Db_port ' => ' 3306 ',//Port
' Db_prefix ' => ' think_ ',//database table prefix
' Db_charset ' => ' UTF8 ',//Database encoding
' Db_debug ' => TRUE,//SQL log can be logged after database debug mode is turned on

If you set a separate database connection information Connection property in the model class, you can use the following array or string:

The code is as follows:
Set up database connection information separately in the model
namespace Homemodel;
Use Thinkmodel;
Class Usermodel extends model{
Define by array
Protected $connection = Array (
' Db_type ' => ' MySQL ',
' Db_user ' => ' root ',
' Db_pwd ' => ' 1234 ',
' Db_host ' => ' 192.168.1.10 ',
' Db_port ' => ' 3306 ',
' Db_name ' => ' thinkphp ',
' Db_charset ' => ' UTF8 ',
);
}

Note: The database connection setting parameters set in the model take the lowercase name of the global configuration.

Or it is defined in a string format:
Database type://Username: password @ database address: Database port/Database name # Character Set
For example:

The code is as follows:
Set up database connection information separately in the model
namespace Homemodel;
Use Thinkmodel;
Class Usermodel extends model{
Define using string methods
protected $connection = ' Mysql://root:1234@192.168.1.10:3306/thinkphp#utf8 ';
}

You can also configure file settings, such as:

The code is as follows:
Database Configuration 1
' Db_config1 ' => Array (
' Db_type ' => ' MySQL ',
' Db_user ' => ' root ',
' Db_pwd ' => ' 1234 ',
' Db_host ' => ' 192.168.1.10 ',
' Db_port ' => ' 3306 ',
' Db_name ' => ' thinkphp ',
' Db_charset ' => ' UTF8 ',
),
Database Configuration 2
' Db_config2 ' => ' Mysql://root:1234@192.168.1.10:3306/thinkphp#utf8 ';

And then define in the model:

The code is as follows:
Set up database connection information separately in the model
namespace Homemodel;
Use Thinkmodel;
Class Usermodel extends model{
Database configuration in the calling configuration file 1
protected $connection = ' db_config1 ';
Or
protected $connection = ' db_config2 ';
}

In addition to specifying the database connection information when the model is defined, we can specify the database connection information when instantiated, and if the M method instantiates the model, it can also support incoming different database connection information, for example:

The code is as follows:
$User = M (' User ', ' other_ ', ' Mysql://root:1234@192.168.1.10/demo#utf8 ');

Represents the instantiated user model, which is connected to the Other_user table of the demo database, with the connection information being configured with the third parameter.
If we have already configured DB_CONFIG2 in the project configuration file, we can also use:
$User = M (' User ', ' other_ ', ' db_config2 ');

The above is the entire content of this article, I hope you can enjoy.

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.