Cakephp calls data tables in different databases

Source: Internet
Author: User
When using the framework cakephp for a project, the following problem occurs: you need to create a database, link the project to the table in the new database, and use loadModel ('testquestion '); however

When using the framework cakephp for a project, the following problem occurs: you need to create a database, link the project to the table in the new database, and use loadModel ('testquestion '); then $ this-> testQuestion-> useDbConfig = 'testbase'; this logic should be correct, but the default database in database configuration is wss, this table does not exist in this database,

Php code
  1. Class DATABASE_CONFIG {
  2. Var $ default = array (
  3. 'Driver '=> 'mysql ',
  4. 'Persistent' => false,
  5. 'Host' => '2017. 168.9.10 ',
  6. 'Login' => 'root ',
  7. 'Password' => '123 ',
  8. 'Database' => 'wss ',
  9. 'Encoding' => 'utf8 ',
  10. 'Prefix' => '',
  11. );
  12. Var $ testBase = array (
  13. 'Driver '=> 'mysql ',
  14. 'Persistent' => false,
  15. 'Host' => '2017. 168.9.10 ',
  16. 'Login' => 'root ',
  17. 'Password' => '123 ',
  18. 'Database' => 'wss _ test ',
  19. 'Encoding' => 'utf8 ',
  20. 'Prefix' => '',
  21. );
class DATABASE_CONFIG {var $default = array('driver' => 'mysql','persistent' => false,'host' => '192.168.9.10','login' => 'root','password' => '123456','database' => 'wss','encoding' => 'utf8','prefix' => '',);        var $testBase = array('driver' => 'mysql','persistent' => false,'host' => '192.168.9.10','login' => 'root','password' => '123456','database' => 'wss_test','encoding' => 'utf8','prefix' => '',);

When the page is displayed, the error 404 is returned. Finally, the solution is found as follows:

Php code
  1. /*
  2. * Model in testBase Library
  3. *
  4. */
  5. Class TestQuestion extends AppModel {
  6. Public $ name = 'preschooltestquestion ';
  7. Var $ useDbConfig = 'testbase ';
  8. Var $ useTable = false;
  9. }
/** Model **/class TestQuestion extends AppModel {public $ name = 'preschooltestquestion '; var $ useDbConfig = 'testbase'; var $ useTable = false ;}

Create a model file named "wss_test" in the models folder and specify the database to use.

By the way: if the default database contains this table, you do not need to create a model file. For example, you can make loadModel ('testquestion') in the master/slave database; then $ this-> testQuestion-> useDbConfig = 'testbase ';

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.