thinkphp cross-Data manipulation configuration examples

Source: Internet
Author: User

In the development of the program, it is unavoidable to encounter across database operations (including the same and different kinds of databases), the use of thinkphp cross-database operations will be much easier

The following is a simple configuration.

1 onfig.php files

' Load_ext_config ' => ' db,db_config ',
Load extended configuration file, multiple files separated by commas
Where the db.php is placed in the MySQL configuration
Db_config placed the configuration of another MySQL database

2 of which db.php contents are as follows

Return Array (
' Db_type ' => ' MySQL ',
' Db_host ' => ' localhost ',
' Db_name ' => ' your_mysql_dbname ',
' Db_user ' => ' your_mysql_user_name ',
' Db_pwd ' => ' 123456 ',
' Db_port ' => ' 3306 ',
' Db_prefix ' => ',
);
db_config.php content is as follows
Return Array (
' Db_config ' =>array (
' Db_type ' => ' MySQL ',
' Db_host ' => ' 127.0.0.1 ',
' Db_port ' => ' 3306 ',
' Db_name ' => ' OA ',
' Db_user ' => ' root ',
' Db_pwd ' => ',
' Db_prefix ' => ' tp_ '
)
);

3 Create a special model under Lib/model

Class Usermodel extends Model {
protected $connection = ' db_config ';
}

4 You can tune each model in action

$user = D (' user ');
Continue with other actions ...
$user->select (); Find all the Users

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.