CakePHP calls data tables in different databases

Source: Internet
Author: User
When you do a project with framework cakephp, you encounter the problem of creating a new database, and then connecting the project to the table in the new library, using Loadmodel (' testquestion '), and then $this->testquestion-> Usedbconfig = ' testbase '; such logic should be no problem, but in the database configuration The default library is WSS, this library is not the table,

PHP code

Class Database_config {var $default = array (' Driver ' = ' mysql ', ' persistent ' and ' = False ') , ' host ' = ' 192.168.9.10 ', ' login ' = ' root ', ' password ' = ' 123456 ', ' d           Atabase ' + ' WSS ', ' encoding ' = ' utf8 ', ' prefix ' and ' = ', '; var $testBase = array (' Driver ' = ' mysql ', ' persistent ' = = False, ' host ' = ' 192.1           68.9.10 ', ' login ' = ' root ', ' password ' = ' 123456 ', ' database ' = ' wss_test ',  ' Encoding ' = ' utf8 ', ' prefix ' = ', '; Class Database_config {var $default = array (' Driver ' = ' mysql ', ' persistent ' = false, ' host ' = ' 192.168.9.10 ', '        Login ' = ' root ', ' password ' = ' 123456 ', ' database ' = ' wss ', ' encoding ' + ' UTF8 ', ' prefix ' and ' = ', '; var $testBase = array (' Driver ' = ' mysql ', ' persistent ' = = False, ' host ' = = ' 192.168.9.10 ', ' login ' = ' root ', ' password ' = ' 123456 ', ' database ' = ' wss_test ', ' encoding ' = ' utf8 ', ' prefix ' and ', ' );

Then the page will be displayed when the 404 error is reported. Finally, a workaround is found, as follows:

PHP code

/   * * Testbase Library Model   * */  class Testquestion extends appmodel{public       $name = ' Preschooltestquestion ';       var $useDbConfig = ' testbase ';       var $useTable = false;   }  /* * Testbase Library model *  */class testquestion extends appmodel{public $name = ' preschooltestquestion '; var $useDbConfig = ' Testbase '; var $useTable = false;}

is to create a model file with the name of the table in the Wss_test library under the Models folder, and then indicate which library to use.

By the way: if the default library has this table, you can not use the model file description. such as in the master-slave library can be so Loadmodel (' testquestion '), and then $this->testquestion->usedbconfig = ' testbase ';

That's what cakephp calls the data tables in different databases, and more about topic.alibabacloud.com (www.php.cn)!

  • 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.