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
- Class DATABASE_CONFIG {
- Var $ default = array (
- 'Driver '=> 'mysql ',
- 'Persistent' => false,
- 'Host' => '2017. 168.9.10 ',
- 'Login' => 'root ',
- 'Password' => '123 ',
- 'Database' => 'wss ',
- 'Encoding' => 'utf8 ',
- 'Prefix' => '',
- );
- Var $ testBase = array (
- 'Driver '=> 'mysql ',
- 'Persistent' => false,
- 'Host' => '2017. 168.9.10 ',
- 'Login' => 'root ',
- 'Password' => '123 ',
- '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' => '',); 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
- /*
- * Model in testBase Library
- *
- */
- Class TestQuestion extends AppModel {
- Public $ name = 'preschooltestquestion ';
- Var $ useDbConfig = 'testbase ';
- Var $ useTable = false;
- }
/** 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 ';